Why Me? (Instead of Form1)

  • Thread starter Thread starter Chack-o
  • Start date Start date
C

Chack-o

This seems to be a "thing" with all the VStudio languages. Why do I
refer to things on the form with the Me qualifier, as in
Me.txtWaffle.Text = "Waffles..."
as opposed to the name of the form itself, as in
Form1.txtWaffle.Text = "Waffles..."
?

This is a difficult topic to search for. I'm reading through the new
Lawrenceville Press VB 2005 textbook, and haven't found an answer
yet...

Nick.
 
In a class you use "me" as well.

If you create a class that represents a object, say an automobile, in
the class you would use Me to refer to itself within the code since you
wouldn't know what the object is called until it is created as an
instance. Although the class is called automobile, you couldn't refer
to itself as automobile.[property] since it would be looking for an
instance of the class.

Whoa, hope that isn't too confusing.
 
The Me keyword is a refence to the class itself. If you use the name of
the reference instead, you are limited to one instance of the class.

If you create two forms from the same class:

Dim menu1 as MenuForm = new MenuForm
Dim menu2 as MenuForm = new MenuForm

Each form can refer to itself using the Me keyword, without having to
know if the main form refers to it as menu1 or menu2.
 
This seems to be a "thing" with all the VStudio languages. Why do I
refer to things on the form with the Me qualifier, as in
Me.txtWaffle.Text = "Waffles..."
as opposed to the name of the form itself, as in
Form1.txtWaffle.Text = "Waffles..."
?
This is a difficult topic to search for. I'm reading through the new
Lawrenceville Press VB 2005 textbook, and haven't found an answer
yet...

Nick.

In this case Form1 refers to a Type (something that has potential to be)
not an instance of the type. For instance, we can do the following:
dim frmKerry = new Form1
frmKerry.txtWaffle.Text = "Waffles..."
frmKerry.Show()

If you were to allow the syntax you mentioned what would happen if you had
2 instances of the Form1 type as follows:

dim frmRestaurant = new Form1
dim frmKerry = new Form1

Then you made the following statement:
Form1.txtWaffle.Text = "Waffles..." which instance would change the value
in the box? We would want to explicity state that it is the restaurant's
waffles we want to change not the politician's.

The trick is inside of the instance, we need a way to refer to the class
that has been instantiated. "Me" was chosen as the best syntax for VB (C#
uses "this"). As was mentioned previously, "me" applies to the current object.
Since everything in .Net inherits from System.Object, forms, classes, etc
can all use the "Me" syntax and call the localized property.

Jim Wooley
http://devauthority.com/blogs/jwooley/archive/2005/09/21/557.aspx
(In case anyone is interested, I am a liberal and have no problems with politician's
waffling)
 
Chack,

A VB Net mainform is still a crazy thing in VSNet. It has some things behind
the scene which are not in C#. The same is that some, as by instance
Herfried won't want to use it that way. The mainform can instance itself to
an object (as you can set in the project properties).

Therefore if you are busy with the mainform you are busy with the class. If
you want to tell inside a class that it has to take things from the class
itself and not from another class/namespace, than you tell "me". The same as
you in C derived languages as JavaScript and C# you use for that "This".

I hope this gives an idea.

Cor
 
Jim said:
If you were to allow the syntax you mentioned what would happen if you had
2 instances of the Form1 type as follows:

dim frmRestaurant = new Form1
dim frmKerry = new Form1

Then you made the following statement:
Form1.txtWaffle.Text = "Waffles..." which instance would change the value
in the box? We would want to explicity state that it is the restaurant's
waffles we want to change not the politician's.

The trick is inside of the instance, we need a way to refer to the class
that has been instantiated. "Me" was chosen as the best syntax for VB (C#
uses "this"). As was mentioned previously, "me" applies to the current object.
Since everything in .Net inherits from System.Object, forms, classes, etc
can all use the "Me" syntax and call the localized property.

I think I'm slowly getting it. Correct me if I'm wrong.
Form1 is a type or a class. We can create instances of Form1
But why is it called Form1 and not Form?
Form1 is _also_ (somewhat confusingly) the Name property of the
default Form1 object that's created when I start a new VB project.
If I have a project with more than one form, then I would use
SecondFormName.whatever when referring to the other form.

Thanks for the help.

Nick.
 
But why is it called Form1 and not Form?

Simple, because Form exist already as class as

System.Windows.Forms.Form

To that is set in your projectproperties an imports path so it takes it
direct.

I hope this helps,

Cor
 
Back
Top