473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to use a public variable

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
3 30002
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
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
* "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Laurence Nuttall | last post by:
I have a class, and a public varible defined in one of the ..VB files of that class. I also have a form in that class, but I cannot reference the variable in the class from the form, even...
5
by: MMSJED | last post by:
I am beginner in using C#, actually I am trying to move from VB6 to C# I need very small help in programming problem my be you will laugh when you get it That simply I have to form let’s say...
2
by: THY | last post by:
Hi, I am having some problem, I declare few variable in a public module and use them in the web application. But after that I found that the variable declared in public variable =...
2
by: Mamatha | last post by:
Hi If any one knows,please tell me how can we declare a public variable in methods of VB.NET like public indx as integer. How can i use that variable in another methods like global...
1
by: D. | last post by:
Hi, i come from classic asp and i'm a little confused with variable declaration in VB. I read: "A Public variable is accessible from any place within the module it was declared or other...
10
by: Phillip Vong | last post by:
Newbie learning in VB.NET 2. I'm creating a simple ASP.NET 2 page and I pulling a querystring from a link and I want to use this querystring over and over again through out the page. Example....
11
by: dgk | last post by:
If I have a class with a public variable, isn't this the same as a private variable with a property? ie, isn't this: Public Class MyTest Public MyVar as String End Class the same as this: ...
1
by: John Kotuby | last post by:
Hi all, In ASP.NET 2.0 I have some pages that are composed of a Master Page and several User controls. In one of the pages I declare a Public variable within the code-behind of a User Control on...
13
by: dougancil | last post by:
I have a public variable that I declared in form1 of an application. I am trying to call that variable in form2 and then pass that variable in a sql query. If I declare Public Class Form1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.