I am writing a program for a class (group project) that is a math quiz game for students. Each form is a new question so I have forms named Question1.cs, Question2.cs, etc.
There are three choices and if the user selects the right choice, its easy enough to close/hide the current form and load the next. However, I would like to make a global form for wrong answers. If I make one form for a wrong answer I only know how to create an event that loads a certain form type, ie if it was wrong on question two the code would say in the button click event on the wrong.cs form:

Question2 Q2 = new Question2();
Q2.show();
this.hide();

But if I wanted to use that same wrong.cs form for a different question, thats where my problem is. I know a decent amount of C++ and general programing concepts so I know what I need to do, but I am not familiar enough with C# yet to do too much. My teacher told us for the sake of her not knowing enough to just make a new form for each wrong question however I don't want to have it taking up all that space and just know how to make an overall better program with a single universal wrong form.
I was thinking about making a global pointer, and when a student clicks a wrong answer, set that pointer to the current form, then when the wrong form loads, and you click back to the previous form, have it load that form that that pointer is pointing to.
But I tried that with the little knowledge that I have and I figured that a pointer of type Form1 is completely different from a pointer of type Form2. How would I get around this?

The most obvious question, is there some reason that you need to use a different form for each question?

Throwing that question aside, What little I know of your design seems to leave quite a bit to desire, so I won't even begin to touch on redesign. I'll just answer your question as asked. Each of your specific forms derives from a Form Class. Thus, you can make your global variable of type Form. Then you can do what you want.

However, I would think that you might want to add some common functionality between forms. In that case, you should create a BaseForm class which inherits from Form. Then, all your other forms will inherit from the BaseForm class. Thus, your global variable would then become a BaseForm class.

By the way, how do you plan to do a global variable in C#?

This article has been dead for over six months. Start a new discussion instead.