Hi,
I'm confused about a few things about classes. I have created a class called Global Variables (see below) I want the whole application to to access the members of this class as required.
This works fine if I make the data members Public Static. I can create an instance of of the class and use them.
But if I make the data members private and add getters I cannot use and instance to access them methods I need to refer to the class name see code below.
Can someone explain whats going on please???
I'm confused about a few things about classes. I have created a class called Global Variables (see below) I want the whole application to to access the members of this class as required.
VB.NET:
Public Class GlobalVariables
Private UName As String = Environment.UserName
Private MachineName As String = Environment.MachineName
ReadOnly Property UserName() As String
Get
Return UName
End Get
End Property
ReadOnly Property MachName() As String
Get
Return MachineName
End Get
End Property
Public Shared Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
End Class
But if I make the data members private and add getters I cannot use and instance to access them methods I need to refer to the class name see code below.
VB.NET:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim GV As New GlobalVariables()
MsgBox(GV.UserName) ' Works
MsgBox(GV.MachName) 'Works
MsgBox(GV.Add(2, 4)) 'Doesnt work !!!!
MsgBox(GlobalVariables.Add(2, 4)) 'Works !!!!
End Sub
End Class