View Single Post
Old June 1st, 2013, 04:03 PM
Rod Stephens's Avatar
Rod Stephens Rod Stephens is offline
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

In the code, we can call Form1 directly, as in:

private void Form1_Load (object sender, EventArgs e)
That code isn't calling the form. It's defining what the form should do if you later trigger the Load event.

It's actually that automatically-generated code I mentioned that creates the form and displays it. It basically calls the form's Show method automatically for you.

When the form is loaded, then it automatically calls the Load event handler for you.

Just typing code into the editor doesn't invoke the code. Some other code somewhere needs to kick things off. Normally that happens when the user clicks a button or something.

But when we want to do a "Button_Click" event that refers to ColorForm, we can't refer to it directly, as for example:


ColorForm is the class. It's sort of like a blueprint or a cookie cutter. It's a type of form not a specific form.

We have to first give it an "alias", and then reference the alias?

That is, we have to make an instance of ColorForm called remoteColorForm, and then reference that?
Almost. An instance of a form is a specific object. When you create an instance, you are making an object.

For example, if you think of the class as a cookie cutter, then making an instance of the class is like making a cookie. If I told you to eat the cookie cutter, that wouldn't make sense. You need to make a cookie and then eat it.

So when the code says Form1, it's talking about the class. Code inside the Form1.cs file is code that defines how forms of this type work.

Behind the scenes, Visual Studio creates and displays an instance of the form. Remember this code:

static void Main()
    Application.Run(new Form1());
That's where the program creates an instance of Form1. It basically makes a cookie.

So it is creating an instance of the form just like you need to with ColorForm. It's just doing it behind the scenes so you don't see it happening.

(Your code can actually do that, too. It can make a new instance of Form1 and display it. Don't do it in the Load event handler, however, or the new form will also create a new form which will create another new form and so on until the system is buried in forms.)

Something like:

Form1 mainForm = new Form1();

private void mainForm_Load (object sender, EventArgs e);
You have this almost exactly right. Visual Studio creates the form for you and then displays it by calling its Show method. The only difference is that you never call mainForm_Load. It executes automatically when the form is loaded.
Ok, I see that in your:

ColorForm remoteColorForm;


remoteColorForm = new ColorForm();

Got that.

But how does the program know that "remoteColorForm" refers to the form in the Designer which is identified as "ColorForm"?
The statement:

ColorForm remoteColorForm;
tells the program that the variable remoteColorForm has the type ColorForm and it remembers that forever more. If you later try to set remoteColorForm equal to a Form1 object or to something like a string or number, Visual Studio will yell at you. Visual Studio knows it is a ColorForm and that's all it can hold. (Or it can hold null, which means "this variable doesn't hold any object at all.")

Now this statement:

remoteColorForm = new ColorForm();
This basically says, "make a cookie of type ColorForm."

Before this statement, remoteColorForm is a variable that could hold a ColorForm but that doesn't yet.

This statement makes a new ColorForm and makes remoteColorForm point to it.

The ideas of classes (like cookie cutters) and instances or objects (like cookies) is fairly confusing until you get used to it. these are also very important ideas it's worth taking some extra time to figure them out.

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)