Using Variables in VB .NET

First,

I come from the old Basic days. Skipped VB5 and VB6 and went straight to VB .NET.

So knowing that, and knowing that I have been reading the dickens out of all sorts of articles, I have a this problem at hand.

I have been working on this for more than a week now and have not been able to get a grasp on the concept of using variables between classes on vb.net.

What I want to be able to do is load up a set of variables and use them in different classes.

The first scenerio is that I want to be able to load information out of the registry and save this to some variables that can be used in any class. May or may not need to change this variable in any class.

The second scenerio is that I need to load sql data (could be a file for that matter) but in this case I am loading SQL data. I need to be able to use this data through out the program. May or may not need to change this variable any in class.

What are the best ways to do this. How do most people writing OO program's handle things like Constants?

Any examples, books or websites would be greatly appreciated.

Thanks

Big T

[1115 byte] By [BigT] at [2008-1-13]
# 1

There are a few ways to do this, I'll explain 2 of them here. I want to note however that you should probably stay away from global variables. So, to use information from the registry in different classes, First option, create your registry class:

Public Class MyRegistryValues

Private myRegistryValue As String

'return/set myRegistryValue here
Public Property RegistryValue() As String
Get,Set
End Property

Public Sub LoadRegistryValues()
End Sub

End Class
If you have classes that absolutely need this information, then maybe you could add a constructor in those classes to deal with this:

Public Class NeedRegistry

Private myRegistryClass As MyRegistryValues

Public Sub New()
End Sub

Public Sub New(registryClass As MyRegistryValues)
myRegistryClass = registryClass
End Sub

Public Function GetRegValue() As String
Return myRegistryClass.RegistryValue
End Sub

End Class

To access this one class from another, just add a property to the above code:

Public Class NeedRegistry

Private myRegistryClass As MyRegistryValues

Public Sub New()
End Sub

Public Sub New(registryClass As MyRegistryValues)
myRegistryClass = registryClass
End Sub

Public ReadOnly Property RegistryClass() As MyRegistryValues

Get
Return myRegistryClass
End Get

End Property

Public Function GetRegValue() As String
Return myRegistryClass.RegistryValue
End Sub

End Class

Now in some other code you can do something like

Dim myReg as New MyRegistryValues
Dim myVar as New NeedRegistry(myReg)

myVar.RegistryClass.RegistryValue
Example 2 is with using Shared. With shared you do not have to create an instance of the class:

Public NotInheritable Class MyRegistryValues

Private Shared myRegistryValue As String

'return/set myRegistryValue here
Public Shared Property RegistryValue() As String
Get,Set
End Property

Public Shared Sub LoadRegistryValues()
End Sub

End Class

Now we just call it like that:

Public Class NeedRegistry

'no need for this anymore
'Private myRegistryClass As MyRegistryValues

Public Sub New()
End Sub

'no need for this either
' Public Sub New(registryClass As MyRegistryValues)
' myRegistryClass = registryClass
'End Sub

Public Function GetRegValue() As String
Return MyRegistryValues.RegistryValue
End Sub

End Class
Forgive my use of names, it was for simplicity. Hope that helps.

JoeSmugeresky at 2007-8-20 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

T: For your first scenario, reading values from the registry and sharing them globally, you can use a static property, e.g.:


Class Globals
Private Shared _Value As String
Public Shared ReadOnly Property Value() As String
Get
If Len(_Value) = 0 Then
_Value = "Value from registry"
End If
Return _Value
End Get
End Property
End Class


Of course, a class containing only static members is equivalent to a VB Module, so you can do this:


Module Globals
Private _Value As String
Public ReadOnly Property Value() As String
Get
If Len(_Value) = 0 Then
_Value = "Value from registry"
End If
Return _Value
End Get
End Property
End Module


If you need to update a value as in your SQL scenario, simply remove the ReadOnly modifier from the property. You can also add Public Constants to the class/module for values that are defined at compile-time.

PhilWeber at 2007-8-20 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Ok I am try to make sense of this... Please Explain this to me.

I setup a form to test this. Two buttons, and a text box.
What is wrong with using this code?

Public Class Form1

Inherits System.Windows.Forms.Form

Public Shared A As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' Call RegData.setme to set the Variable "BB"

RegData.setme()

' Set A to equal BB from Class1

A = RegData.BB

TextBox1.Text = A ' Show me on the form

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

' Call RegData.setme2 to set the Variable A in this class

RegData.setme2()

TextBox1.Text = A ' Show me that it changed it.

End Sub

End Class

Now I created a Class file called Class 1. It contains.

Public Class RegData

Public Shared BB As String

Public Shared Sub setme()

BB = "I AM SET"

End Sub

Public Shared Sub setme2()

Form1.A = "GOBBLE GOBBLE"

End Sub

End Class

Does this make these classes open up to all other programs running on the system?
Thanks,

Big T

BigT at 2007-8-20 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Joe Smugeresky wrote:

There are a few ways to do this, I'll explain 2 of them here. I want to note however that you should probably stay away from global variables. So, to use information from the registry in different classes, First option, create your registry class:
Example 2 is with using Shared. With shared you do not have to create an instance of the class:

Joe,

With out understanding yet, why would I choose one method over the other?

Thanks,

Big T

BigT at 2007-8-20 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
I don't think you quite understand so I'll fix the code you posted:


Public Class Form1 : Inherits System.Windows.Forms.Form

Private _A As String

Public Sub Button1_Click(sender As Object. e As EventArgs) Handles Button1.Click

RegData.BB = "I AM SET"
TextBox1.Text = RegData.BB

End Sub

Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

_A = "GOBBLE GOBBLE"
RegData.BB = _A
TextBox1.Text = RegData.BB

End Sub

End Class
Public Class RegData

Private Shared _BB As String

Public Shared Property BB() As String

Get
Return _BB
End Get

Set(ByVal Value As String)
_BB = Value
End Set

End Property

End Class


Hope that helps.

JoeSmugeresky at 2007-8-20 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Joe Smugeresky wrote:
I don't think you quite understand so I'll fix the code you posted:


Public Class Form1 : Inherits System.Windows.Forms.Form

Private _A As String

Public Sub Button1_Click(sender As Object. e As EventArgs) Handles Button1.Click

RegData.BB = "I AM SET"
TextBox1.Text = RegData.BB

End Sub

Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

_A = "GOBBLE GOBBLE"
RegData.BB = _A
TextBox1.Text = RegData.BB

End Sub

End Class
Public Class RegData

Private Shared _BB As String

Public Shared Property BB() As String

Get
Return _BB
End Get

Set(ByVal Value As String)
_BB = Value
End Set

End Property

End Class


Hope that helps.

Joe,

You are correct, I am having trouble understanding this. I think what I am having problems with is understanding the CLASS its self. Correct me if I am wrong, but in my minds eye, I see the class as a seperate sub-routine. If I want to execute code within this class I must call it. I am having troubles understanding the variable scopes which is a matter of time. As for now I understand that Private allows just that class, function or sub to have access to the varaibles from which they were defined. I thought I could use the Public Shared to be able to access the variables defined in a class to be used outside of the classes as you saw in my sample.

I was trying in my sample to show you all that I think I need to have a class that when called sets all of my "GLOBAL", for a lack of a better term variables, (i.e. variables I can use throught my program which will more be data loaded from my registry hive.) Also I had planned to use a class to load data from an SQL database and assign them to variables that other classes when called could use.

So not understanding classes here is my problem I know that and your fix to my sample just confused me. This part made no sense to me:

RegData.BB = "I AM SET"
TextBox1.Text = RegData.BB

Does this call the code in the RegData class?

And this code:

_A = "GOBBLE GOBBLE"
RegData.BB = _A
TextBox1.Text = RegData.BB

Please explain the underscore A "_A", I have not seen this before. As is the first one, does the RegData.BB = _A here also call the code in the RegData class?

Do I need to have the GET Property in my RegData class for EVERY variable that I want to access outside of my class (e.g. I load up 25 fields out of the SQL Database, Do I need to have 25 Seperate Property and Gets)?

I hate to sound this stupid on this but for some reason this Object thing is not sinking in just yet. Gonna go try to find another book. The first I had just talked about concepts but never no code or explanations.

Thanks for your time and help.

I appreciate it.

Big T

BigT at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
BigT, let me explain a little further,

RegData.BB = "I AM SET"
TextBox1.Text = RegData.BB

This code does call the property in the RegData Class. First:
RegData.BB = "I AM SET" this code sets the var _BB in the RegData class to "I AM SET". The second line there TextBox.Text = RegData.BB is now getting the value of RegData.BB which is "I AM SET" and returns "I AM SET" right to the textbox property Text. The underscore before the var name is just used by preference to denote member variables, you can name your private variables whatever you want, A would have been just fine also if that is what you wanted.

As far as a get and a set for every property, well, that depends on your class, I don't know if 25 properties would be a good thing. You can however return a List<T> or List of type, or an array, or a collection from a method in your class rather than using 25 properties.

Joe

JoeSmugeresky at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...