Connecting Tech Pros Worldwide Help | Site Map

How to use a public variable

Tor Inge Rislaa
Guest
 
Posts: n/a
#1: Nov 20 '05
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



EricJ
Guest
 
Posts: n/a
#2: Nov 20 '05

re: How to use a public variable


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" <tor.inge.nospam@rislaa.no> wrote in message
news:BZshb.36577$Hb.570175@news4.e.nsc.no...[color=blue]
> 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
>
>
>[/color]


Tom Shelton
Guest
 
Posts: n/a
#3: Nov 20 '05

re: How to use a public variable


On 2003-10-10, Tor Inge Rislaa <tor.inge.nospam@rislaa.no> wrote:[color=blue]
> 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
>
>
>[/color]

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]
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#4: Nov 20 '05

re: How to use a public variable


* "Tor Inge Rislaa" <tor.inge.nospam@rislaa.no> scripsit:[color=blue]
> 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.[/color]

The code doesn't work because of the lack of default instances in VB.NET.
[color=blue]
> 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[/color]

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>
Closed Thread