views:

82

answers:

2

In vb.net, you can address a public variable from another form using the form name along with the variable.

form2.show
form2.k = 3

However, if you use a form variable to show an instance of the form, you must use that variable name to address the public variable. Two instances of the same form are displayed in the following example. The public variable k is assigned a value of 3 only in the first instance of the form, the one from form2.show. frm.k can be used to assign a value to the other form.

dim frm as new form2
form2.show
frm.show
form2.k = 3

My question: Assuming only one instance of the form is shown in the application, is it reliable to address a public variable using the form name (form2.k), or is it better to show the form with a form variable and use that to refer to the instance of the form (frm.k)? Would the same answer apply to a property as well as a public variable?

+2  A: 

Little bit of clarification here. In the case of using the form name to access the variable you are still using an instance to access the variable. It's a feature of VB.Net called the default instance. Essentially VB.Net will maintain a single instance per form type. Under the hood it will translate your code to access this instance variable. This was a very popular feature of VB6 which was ported to VB.Net (in 2005 I believe).

The case where a form is shown once is the exact case this feature was designed for. So yes it's reliable to use it to access the value. This applies to any instance member.

This is of course assuming everything is single threaded.

JaredPar
A: 

Definitely #2, use the instance variable. I didn't know that the other syntax still worked! That was a leftover from VB4 that was moved to VB6 for compatibility. I thought they dumped it in .net. Don't think it would even work in C#.

DancesWithBamboo
That other syntax is pretty convenient, and seems to be the most popular method in vb to show a form. Why should a vb developer add a form variable when it's not required? I'm not trying to argue -- I am just looking for your rationale.
xpda
A form variable is required by the framework and VB is just making one named the same as your form class behind the scenes for you. All you are doing is making that action explicit in the code.It is most popular because it had been around the VB community for over 10 years! Tough to change old habits.If nothing else I would use the explicit version for uniformity and readability. Where else can you get away with using a class name as an instance variable in .net
DancesWithBamboo
I would also argue that form2.show() and form2.k look disturbing similar to static methods, which of course they are not.
DancesWithBamboo
You are right, it is preferable to use the instance variable. The other syntax does look like a static method call and is rather nasty. BTW it doesn't work in C# and it was originally dumped in VB.Net, it was readded later as a minor attempt to simplify migration of VB6 code to VB.Net.
MarkJ