Re : Global Variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BB206

    #1

    Re : Global Variable

    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!


  • Martin H.

    #2
    Re: Re : Global Variable

    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.Varia ble1 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

    Comment

    • BB206

      #3
      Re: Re : Global Variable

      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!

      "Martin H." <hkshk@gmx.ne t>
      ???????:4653f91 4$0$29300$9b622 d9e@news.freene t.de...
      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.Varia ble1 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

      Comment

      • Martin H.

        #4
        Re: Re : Global Variable

        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.Vari able1.ToString)
        End Sub
        End Class

        As you can see, the sub "Foo" references clsGLOBAL.Varia ble1 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.Varia ble1.ToString)
        End Sub
        End Class

        Best regards,

        Martin

        BB206 wrote:
        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!
        >
        "Martin H." <hkshk@gmx.ne t>
        ???????:4653f91 4$0$29300$9b622 d9e@news.freene t.de...
        >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.Varia ble1 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
        >
        >

        Comment

        • BB206

          #5
          Re: Re : Global Variable

          I got it, Thanks a lot!

          "Martin H." <hkshk@gmx.ne t>
          ???????:46560a9 e$0$10960$9b622 d9e@news.freene t.de...
          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.Vari able1.ToString)
          End Sub
          End Class
          >
          As you can see, the sub "Foo" references clsGLOBAL.Varia ble1 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.Varia ble1.ToString)
          End Sub
          End Class
          >
          Best regards,
          >
          Martin
          >
          BB206 wrote:
          >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!
          >>
          >"Martin H." <hkshk@gmx.ne t>
          >???????:4653f9 14$0$29300$9b62 2d9e@news.freen et.de...
          >>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
          >>>applicatio n 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.Varia ble1 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
          >>

          Comment

          Working...