tags:

views:

15

answers:

1

VB.Net 2005
I have a now closed Dialog1. To get information from the Dialog1 from within a module I need to use
Dim oDialog1 as Dialog1 = New Dialog1.

VB.Net 2008
I have a still open Dialog1. To get information from the Dialog1 from within a module I need to use
Dim oDialog1 as Dialog1 = Dialog1.

VB.Net 2005 does not compile using Dim oDialog1 as Dialog1 = Dialog1 and insists on NEW

What is going on and why do I need the different initialisation syntax?

A: 

Dialog1 is the type of the object you are creating.

Dim oDialog1 as Dialog1 = Dialog1

is the same as saying

MyCat is a Cat, and it's a Cat.

Doesn't quite make sense.

If you need to know about how many legs cats have, or if cats are furry, then you can say Cat.CountLegs, but you can't say Cat.GetName or Cat.Age because you don't know which cat you're talking about.

It's the same with your Dialog.

Dim oDialog1 as Dialog1 = Dialog1

is not referring to any particular Dialog, just Dialog1's in general, which doesn't make sense (and also shouldn't compile in VB.NET 2008).

Where as

Dim oDialog1 as Dialog1 = New Dialog1

is giving you a brand new Dialog1, called oDialog1. Everything you ask about oDialog1 will give you generic, default about Dialog1 object.

Dialog1 will not have a position, because it doesn't exist yet. But because you've created a new instance of one by using the NEW keyword, oDialog1 will be your first object of the Dialog1 type - you can give it a position, etc.

If you call

Dim oDialog2 as Dialog1 = New Dialog1

Then you'll have two Dialog1's - each with a separate position, etc.

It will help make a bit more sense if you give Dialog1 and oDialog1 better names, such as UserConfirmationDialog and confirmExit, respectively.

It will then become something like

Dim confirmExit as UserConfirmationDialog = New UserConfirmationDialog.

and possibly

Dim confirmDelete as UserConfirmationDialog = New UserConfirmationDialog.
Michael Rodrigues
Thankyou Michael. I have changed the Dialog to a Form with the same results. If I use Dim oForm1 = NEW Form1 it does not retrieve (for example) the oForm1.RadioButton1.Checked status. If I drop the NEW it correctly retrieves the RadioButton status?
Ah, this is because your application is creating a NEW form for you when you start the application. This is a bit tricky. Form1 is the type of the object (i.e. you can start as many Form1's as you want). Your users need to see something when the program starts so the runtime creates an instance of a Form1 for you automatically, and for whatever reason it *also* calls the new instance "Form1". That's what's causing the confusion. You've got two different things with the same name - a template and an actual form. Computer understands what's going on, but it's confusing for us.
Michael Rodrigues