By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

How to use a public variable

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Tor Inge Rislaa" <to*************@rislaa.no> wrote in message
news:BZ*******************@news4.e.nsc.no...
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

Nov 20 '05 #2

P: n/a
On 2003-10-10, Tor Inge Rislaa <to*************@rislaa.no> wrote:
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/...hottips/lobel/
http://www.ftponline.com/Archives/pr...111/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 :)

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3

P: n/a
* "Tor Inge Rislaa" <to*************@rislaa.no> scripsit:
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'.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.