Re : Global Variable

B

BB206

Is there any place or method to hold or pass some global variables, ie user
name, user level..., that can be shared within the whole application which
has a lot of dll and user defined components?

I am work on Visual Basic 2002.

Thanks a lot!
 
M

Martin H.

Hello BB206,
Is there any place or method to hold or pass some global variables, ie user
name, user level..., that can be shared within the whole application which
has a lot of dll and user defined components?

When using global variables, please keep in mind that it can be changed
anywhere in your code. If it is that you mainly need to read it's value
then it would be better to make a class and a property, e.g.

Public Class clsGLOBAL
Private Shared vVariable1 As Integer = 10

Public Shared ReadOnly Property Variable1() As Integer
Get
Return vVariable1
End Get
End Property
End Class


This way, you can access your variable with clsGLOBAL.Variable1 and you
don't have to worry that by mistake you change the value of that
variable. Still, in the class clsGLOBAL you can change it as required.

Best regards,

Martin
 
B

BB206

Thank you Martin, But how to pass the class from one class to another class.
I had written some class dlls that has no way to pass the class between
them.

Thanks!
 
M

Martin H.

Hello BB206,

here's an example:

Public Class clsGLOBAL
Private Shared vVariable1 As Integer = 10

Public Shared ReadOnly Property Variable1() As Integer
Get
Return vVariable1
End Get
End Property
End Class

Public Class Test
Private Sub Foo()
MsgBox (clsGLOBAL.Variable1.ToString)
End Sub
End Class

As you can see, the sub "Foo" references clsGLOBAL.Variable1 without the
need of an instance. This is, because it is a shared property. The side
effect of shared properties is that they require a shared variable
behind (vVariable1 is also "Shared"). "Shared" variables can only be
used in "Shared" subs/functions.

If you don't want to use "Shared" variables, you have to create an
instance of that class.

Public Class clsGLOBAL2
Private vVariable1 As Integer = 10

Public ReadOnly Property Variable1() As Integer
Get
Return vVariable1
End Get
End Property
End Class

Public Class Test
Private Sub Foo()
dim ClsGlbl2 As New clsGLOBAL2
MsgBox (ClsGlbl2.Variable1.ToString)
End Sub
End Class

Best regards,

Martin
 
B

BB206

I got it, Thanks a lot!

Martin H. said:
Hello BB206,

here's an example:

Public Class clsGLOBAL
Private Shared vVariable1 As Integer = 10

Public Shared ReadOnly Property Variable1() As Integer
Get
Return vVariable1
End Get
End Property
End Class

Public Class Test
Private Sub Foo()
MsgBox (clsGLOBAL.Variable1.ToString)
End Sub
End Class

As you can see, the sub "Foo" references clsGLOBAL.Variable1 without the
need of an instance. This is, because it is a shared property. The side
effect of shared properties is that they require a shared variable behind
(vVariable1 is also "Shared"). "Shared" variables can only be used in
"Shared" subs/functions.

If you don't want to use "Shared" variables, you have to create an
instance of that class.

Public Class clsGLOBAL2
Private vVariable1 As Integer = 10

Public ReadOnly Property Variable1() As Integer
Get
Return vVariable1
End Get
End Property
End Class

Public Class Test
Private Sub Foo()
dim ClsGlbl2 As New clsGLOBAL2
MsgBox (ClsGlbl2.Variable1.ToString)
End Sub
End Class

Best regards,

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top