Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 31st, 2013, 11:33 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default ColorForm Figure 9-4 and associated lesson

I had a LOT of trouble trying to understand the code for this one (top of page 108), even though I've read through two other C# training books (with minimal success)

I am somewhat comfortable with the concept:

Object dog = new Object();

The problem I had is that the lesson in your book uses the terms "ColorForm" , "RemoteForm", "remoteColorForm" in various ways.

The namespace is named "RemoteForm" and the Form1 has a Text = "RemoteForm"

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" !!!

This was way too much duplication for me to figure out what was doing what!!

If the form has a (Name) = ColorForm, how can the code call it up by using "remoteColorForm" ??

Where is the secondary form associated with the name "remoteColorForm"??

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!

But I don't know how.

The ColorForm form has a (Name) = ColorForm, and Text= ColorForm, so how does calling it up as "dummyForm" work??

If:

ColorForm dummyForm = new ColorForm();

should not the (Name) of the secondary form be "dummyForm" ??


In your code, why does " remoteColorForm.Show" display a form that is not named remoteColorForm? Why does this not throw an error??

thanks
  #2 (permalink)  
Old June 1st, 2013, 10:48 AM
Rod Stephens's Avatar
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!)
  #3 (permalink)  
Old June 1st, 2013, 12:31 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, maybe I'm being dense here (it happens).

Your Project has a form named Form1, and then you click Project, Add Windows Form. It asks for a name and you name it ColorForm.cs.

This opens the second form in the Designer, with Properties: Text = "ColorForm" and (Name) = ColorForm.

Got that.

Now you said "The code creates an instance of it and names if remoteColorForm."

Ok, I see that in your:

ColorForm remoteColorForm;

and

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"?

Is there some code in the background, similar to what created Form1, that equates remoteColorForm to the form Named ColorForm?

When code executes in Form1, how does it know to change the Properties of ColorForm?

where does "ColorForm" = "remoteColorForm" ?

To me, that's like saying "make the dog's tail wag" and having the cat's tail wag instead!
  #4 (permalink)  
Old June 1st, 2013, 01:20 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm still trying to understand this.

Maybe I'm not asking my question correctly?

In the Solution, we have two Forms: Form1.cs and ColorForm.cs

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

private void Form1_Load (object sender, EventArgs e);

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.Show();

}

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?

If we have both Form1 and ColorForm in the Solution Explorer, why can we call one by it's "true" name, but have to make up an alias to call the other one?

Alternately, why don't we have to alias Form1 before we call it?

Something like:

Form1 mainForm = new Form1();

private void mainForm_Load (object sender, EventArgs e);

???
  #5 (permalink)  
Old June 1st, 2013, 04:03 PM
Rod Stephens's Avatar
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

Quote:
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.

Quote:
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.Show();

}
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.

Quote:
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:

Code:
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    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.)

Quote:
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.
=====
Quote:
Ok, I see that in your:

ColorForm remoteColorForm;

and

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:

Code:
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:

Code:
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

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #6 (permalink)  
Old June 1st, 2013, 06:04 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for sticking with me ... I know this is getting long-winded.

Let me back-up.

Let's say I want to create a Project with two forms in it.

I open New Project and give it a name.

It has a Form1.cs in it.

Now, if I "Add New Form" and use the default name(Form2.cs) for the new form, I now have a Form1 and Form2 in Solution Explorer.

If I run Debug, I see Form1, but not Form2 ... why not?
  #7 (permalink)  
Old June 2nd, 2013, 10:47 AM
Rod Stephens's Avatar
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

When you create the project, by default Visual Studio defines Form1 (the cookie cutter). It actually doesn't make a Form1 yet.

It also defines that piece of code that creates an instance of Form1 (the cookie) and displays it. If you run the program, that form is displayed.

Then you define another type of form (cookie curter), Form2.

Now you have two cookie cutters but there's no code that creates an instance of Form2 and no code to display it. If you want to see an instance of Form2, you need to write your own code because Visual Studio doesn't do that for you.

(Why? Well if you make a project you're going to need to display something or the user won't see anything. So Visual Studio displays Form1 by default. But if you create your own form, Form2, Visual Studio has no idea what you want to do with it. You might want to display it right away or you might want to display it as a dialog only under special circumstances. So it doesn't automatically display that one.)
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #8 (permalink)  
Old June 2nd, 2013, 01:26 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's an interesting way to put it ... That Form1.cs and Form2.cs are the cookie cutters.

I guess I was expecting there to be something in System.Windows.Forms called "Form", which would be the cookie cutter, and Form1.cs and Form2.cs to be the cookies.
  #9 (permalink)  
Old June 3rd, 2013, 10:40 AM
Rod Stephens's Avatar
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

There is a Form class, too, from which all of your forms inherit. You wouldn't to make an instance of one, though, because it doesn't have anything on it. Instead you make a subclass Form1 or ColorForm that has what you want on it. Then you can make any number of Form1 or ColorForm objects.

You'll learn more about classes in Lessons 23 through 28.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Figure 6.2 DrPurdum BOOK: Beginning ASP.NET 4.5 : in C# and VB 5 April 8th, 2013 11:24 AM
Figure 4-2 KPN BOOK: Beginning JavaScript and CSS Development with jQuery 0 November 11th, 2009 03:44 PM
I can't figure this out. aaronmuslim XSLT 4 March 20th, 2008 11:13 AM



All times are GMT -4. The time now is 04:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.