View Single Post
  #2 (permalink)  
Old June 1st, 2013, 10:48 AM
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
Default

Let's start with this:

Quote:
Object dog = new Object();
This code creates an object of type Object. Object is the class. The variable that refers to the object is named dog.

Quote:
The problem I had is that the lesson in your book uses the terms "ColorForm" , "RemoteForm", "remoteColorForm" in various ways.
Some of this is created automatically when you make the project. You can change it but it's less confusing to just use what it created for you.

When you create a project named "RemoteForm," Visual Studio makes a namespace called RemoteForm. You usually don't need to use the namespace directly until you're writing more advanced programs.

Visual Studio also creates an initial class named Form1. This is a type of form.

It also creates code behind the scenes that creates an instance of the form and displays it. The code looks like this:

Code:
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
In the last line you can see where it creates the instance of the Form1 class. The Application.Run statement displays it.

Quote:
The namespace is named "RemoteForm" and the Form1 has a Text = "RemoteForm"
As mentioned above, Visual Studio named the namespace. I gave the form the caption RemoteForm because that's what I wanted it to display so you would know it was the RemoteForm project.

Quote:
The secondary form has a resource name of ColorForm.cs, and a (Name) = ColorForm, and Text = ColorForm, however in the Form1.cs code it is referred to a "remoteColorForm" ... even though the primary form is titled "RemoteForm" !!!
Next I added a new form type to the project and named it ColorForm because it is a form for getting a color. The code creates an instance of it and names if remoteColorForm. The form's identifying part is "remote" and the ending "ColorForm" tells you what kind of object it is.

(This is not my preferred naming convention but it's what most C# developers use. I prefer to use dlg or frm to indicate a dialog or form so I would normally call this object something like dlgColor meaning a dialog that gets a color.)

At this point, however, the main form's title has nothing to do with the second form.

Quote:
If the form has a (Name) = ColorForm, how can the code call it up by using "remoteColorForm" ??
Here you're mixing up the name of the form's class and the name of the instance of the class. In your initial example, it's like confusing Object and dog.

Here ColorForm is the class and remoteColorForm is an instance of that class.

In the most common naming convention, the end of an object's name gives the object's class (as in this example).

Quote:
Where is the secondary form associated with the name "remoteColorForm"??
The variable remoteColorForm is the instance of the ColorForm class. It's the equivalent of "dog" in your example.

Quote:
Finally I took your code:

"// The remote form we will manipulate.
ColorForm remoteColorForm;

// Create and display the remote form.
private void Form1_Load(object sender, EventArgs e)
{
remoteColorForm = new ColorForm();
remoteColorForm.Show();
}

// Make the color form red.
private void redButton_Click(object sender, EventArgs e)
{
remoteColorForm.BackColor = Color.Red;
remoteColorForm.ForeColor = Color.Pink;
} "

and replaced the "remoteColorForm" with "dummyForm" (which I made up), like this:

"
// The remote form we will manipulate.
ColorForm dummyForm;

// Create and display the remote form.
private void Form1_Load(object sender, EventArgs e)
{
dummyForm = new ColorForm();
dummyForm.Show();
}

// Make the color form red.
private void redButton_Click(object sender, EventArgs e)
{
dummyForm.BackColor = Color.Red;
dummyForm.ForeColor = Color.Pink;
} "

and the Solution still worked!
Yes. You just changed the name of the instance of the ColorForm that you created. Basically you changed "dog" to "cat." As long as you changed the name everywhere, it should work.

Feel free to change the name to something that makes more sense to you if you think the names used in the examples are confusing. Just look for error messages if you forget to change the name in one place. For example, Visual Studio would say something like "remoteColorForm is undefined" if you leave that name in there somewhere. You can then fix the name to the new one and you should be ready to go again.

Quote:
If:

ColorForm dummyForm = new ColorForm();

should not the (Name) of the secondary form be "dummyForm" ??
In that line of code, the type of form is still ColorForm because you didn't change that. You just changed the name of the instance of the form. (Changed "dog" to "cat.")

Quote:
In your code, why does " remoteColorForm.Show" display a form that is not named remoteColorForm? Why does this not throw an error??
The statement remoteColorForm.Show() means, "for the object named remoteColorForm, call its Show method" and the Show method displays the form.

If you change the name of the instance of the form to dummyForm, then you would display it with dummyForm.Show().

After you create an instance of an object, you usually don't need to use the name of its class any more. This statement makes an instance of the ColorForm class:

Code:
ColorForm myform = new ColorForm();
Now this code displays the new instance of the form:

Code:
myform.Show();
I hope that helps.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

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