Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Public variable

Hi Experts,

How to create a public variable which can be used only in the form. Suppose I have form1.aspx. How can I define a public variable which can be used only in form1 .no other form should able to access it.
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Just declare it in the class module of Form1, and mark it Private:

Private MyVariable As Integer
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Thank you for your reply. When it is private is it shared by only one instance or multiple instance? I think it for one instance only, just want to conform.
A private variable is not shared at all.  If you are wanting to *Share* a variable (in VB terms and .NET definitions), then you would mark it as Shared.  Methods, Properties and Fields defined within Modules are inherently Shared.

That being said, if you instantiate a form (which is a Class object); e.g. -
Dim frm As New Form1

Open in new window

Then the object that instantiated frm has access to frm.  If frm, by definition of Form1, contains a public field/property called someRandomThing, then the object that instantiated frm has access to this field/property.

-saige-
Hi,

When I declare as  "Private MyVariable As Integer" still I can not access it in all the sub of that  class. But When I declared as  
"Private Shared MyVariable As Integer" then I can access it from all the subs. Is that the right way?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial