How to use a public variable

T

Tor Inge Rislaa

How to use a public variable

In VB.6.0 I could in the declaration part of a form declare a public
variable, then assign a value to the variable, open a new form and address
the variable and read it's value in my new form.



Form1:

Option Explicit

Public MyVar As String

Private Sub Form_Load()
MyVar = "Some value"
End Sub


Form2:

Private Sub Form_Load()
Dim MyVar2 As String
MyVar2 = Form1.MyVar

End Sub

How is this done in VB.NET


T.I. Rislaa
 
E

EricJ

if im not wrong you can do that 2 but normally you should add a module to
your project and declare the public vars there (again if im not wrong you
could do this in vb6 to)

'code
Module var



Public gUser As New clsUser

Public gdsMain As DataSet

Public gstrCnn As String



Public fmLogin As frmLogin

Public fmMain As frmMain




Public Sub InfoBoodschap(ByVal msg As String, ByVal titel As String)

MessageBox.Show(msg, titel, MessageBoxButtons.OK,
MessageBoxIcon.Information)

End Sub

End Module
 
T

Tom Shelton

How to use a public variable

In VB.6.0 I could in the declaration part of a form declare a public
variable, then assign a value to the variable, open a new form and address
the variable and read it's value in my new form.



Form1:

Option Explicit

Public MyVar As String

Private Sub Form_Load()
MyVar = "Some value"
End Sub


Form2:

Private Sub Form_Load()
Dim MyVar2 As String
MyVar2 = Form1.MyVar

End Sub

How is this done in VB.NET


T.I. Rislaa

I'm a little drowsy right now - and was about to stumble up to bed....
So, hopefully what I'm about to say is coherent. If not, well sorry :)

Anyway, what your doing is not possible in VB.NET for the simple fact
that VB.NET does not create default instances of your forms as it did in
VB.CLASSIC. In other words, to access members of one form in another
form, you have to either have an explicit reference (via a constructor
or a property) or you have to create a global reference manually in a
module. The way you do this really depends on the relationship between
forms. Anyway, here is a couple of links that may help you out:

http://www.fawcette.com/vsm/2002_12/online/hottips/lobel/
http://www.ftponline.com/Archives/premier/mgznarch/vbpj/2001/11nov01/qa0111/qa0111.asp

Anyway, both of these have proposed methods of mimicking the VB.CLASSIC
behavior. Mind you, I haven't made a thorough read of the first
article, just skimed it really - but it looked decent enough... If not
I apologize - but let me know what you think :)
 
H

Herfried K. Wagner [MVP]

* "Tor Inge Rislaa said:
In VB.6.0 I could in the declaration part of a form declare a public
variable, then assign a value to the variable, open a new form and address
the variable and read it's value in my new form.

The code doesn't work because of the lack of default instances in VB.NET.
Form1:

Option Explicit

Public MyVar As String

Private Sub Form_Load()
MyVar = "Some value"
End Sub


Form2:

Private Sub Form_Load()
Dim MyVar2 As String
MyVar2 = Form1.MyVar

End Sub

How is this done in VB.NET

A _very simple_ (and ugly) solution is to put the variables into a
Module:

\\\
Public Module Globals
Private m_MyVar As String

Public Property MyVar() As String
Get
Return m_MyVar
End Get
Set(ByVal Value As String)
m_MyVar = Value
End Set
End Property
End Module
///

You can access the property by typing 'Globals.MyVar'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top