Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public variable on forms 1 & 2

Status
Not open for further replies.

northernbloke

Programmer
Aug 9, 2000
29
0
0
GB
Newbie question, so my appologies.

Have I missed something glaringly obvious??

if I declare a variable on Form1 as public, shouldn't I be able to see it on Form2 ??

I can put the declaration onto a standard module, but is that the way it should be ??

sorry, I may be misreading this stuff.
 
northernbloke,

Type the name of the form and the dot: Form1.YourPublicVariable

It is better to declare public variables in the module rather then inside a form.

Vladk
 
OK,

This must be a missprint, as my book definately says to put the declaration in Form1.

I'll carry on using Modules for Public vars.

Thanks,
N'Bloke.
 
Think of the form as a class.
A public variable 'creates' a visible property/attribute.
As vladk says, to use the property of a class, you need to prefix it with the name of the object.

VB always allows you to refer to a 'system' instance of any form using the form name.
eg Form1.show works nicely.

But you may want several copies of the form, and this is when it looks more like a class.

Dim x as form1
dim y as form1
set x= new form1
set y= new form1


x.theproperty = 6
y.theproperty =9

Now, each instance has its own value for the variable you thought should be global.

A 'true' global variable gets declared as public in a module.

And you can reference it using module1.thevariable if you like!



 
You are largely right...


but it is possible to have many different instances of form1,

That is to say that there could be many different values of your variable (that can go in and out of scope)

so you need to qualify the variable with which instance you want to use...

thus
Code:
form1.MyPublicVariable = myNewValue

If you want 1 value visible to all areas of your program tyou will need to put it in a module.

As a matter of style, I prefer to use get/let statements for forms to create a public property of the form.

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
all beat me to it!

when Istarted there were no other posts!(but ihave dealt with aupport call inbetween)

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top