how to initialize shared variable

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

    #1

    how to initialize shared variable

    Dear all,
    how can I initialize shared variable?
    i.e
    class A
    private shared B as boolean
    end class

    in C++ I would write in the global scope A::B=true
    What is the proper way in vb


  • Herfried K. Wagner [MVP]

    #2
    Re: how to initialize shared variable

    "Boni" <oilia@nospam > schrieb:[color=blue]
    > how can I initialize shared variable?
    > i.e
    > class A
    > private shared B as boolean
    > end class[/color]

    \\\
    Private Shared B As Boolean = True
    ///

    Alternatively you can add a shared constructor and initialize the variable
    there:

    \\\
    Private Shared B As Boolean

    Shared Sub New()
    B = True
    End Sub
    ///

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Boni

      #3
      Re: how to initialize shared variable

      Hi Herfried,
      thanks for the answer, the drawback of your approaches seems to be, that the
      B will be reset to true each time, when the instanstance of the object is
      creates. So if I have
      dim OA as new A
      oA.B=false
      dim OA1 as new A 'this reset B in both instances to true!!!
      What I want is initial initialization of the shared variable. There should
      be some sintax for that in Visual Basic. i.e. A::B=false.

      Thanks a lot,
      Boni
      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb im Newsbeitrag
      news:%23m8wQ0eY FHA.796@TK2MSFT NGP09.phx.gbl.. .[color=blue]
      > "Boni" <oilia@nospam > schrieb:[color=green]
      >> how can I initialize shared variable?
      >> i.e
      >> class A
      >> private shared B as boolean
      >> end class[/color]
      >
      > \\\
      > Private Shared B As Boolean = True
      > ///
      >
      > Alternatively you can add a shared constructor and initialize the variable
      > there:
      >
      > \\\
      > Private Shared B As Boolean
      >
      > Shared Sub New()
      > B = True
      > End Sub
      > ///
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: how to initialize shared variable

        "Boni" <oilia@nospam > schrieb:[color=blue]
        > thanks for the answer, the drawback of your approaches seems to be, that
        > the B will be reset to true each time, when the instanstance of the object
        > is creates. So if I have
        > dim OA as new A
        > oA.B=false
        > dim OA1 as new A 'this reset B in both instances to true!!![/color]

        No, that's not the case. Shared variables are only initialized once.

        \\\
        MsgBox(Test.A) ' 'True'.
        MsgBox(Test.B) ' 'True'.
        Test.A = False
        Test.B = False
        MsgBox(Test.A) ' 'False'.
        MsgBox(Test.B) ' 'False'.
        Dim t As New Test
        MsgBox(Test.A) ' 'False'.
        MsgBox(Test.B) ' 'False'.
        ..
        ..
        ..
        Public Class Test
        Public Shared A As Boolean
        Public Shared B As Boolean = True

        Shared Sub New()
        A = True
        End Sub
        End Class
        ///

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Boni

          #5
          Re: how to initialize shared variable

          Thank you for your help
          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb im Newsbeitrag
          news:etVIDLjYFH A.2684@TK2MSFTN GP09.phx.gbl...[color=blue]
          > "Boni" <oilia@nospam > schrieb:[color=green]
          >> thanks for the answer, the drawback of your approaches seems to be, that
          >> the B will be reset to true each time, when the instanstance of the
          >> object is creates. So if I have
          >> dim OA as new A
          >> oA.B=false
          >> dim OA1 as new A 'this reset B in both instances to true!!![/color]
          >
          > No, that's not the case. Shared variables are only initialized once.
          >
          > \\\
          > MsgBox(Test.A) ' 'True'.
          > MsgBox(Test.B) ' 'True'.
          > Test.A = False
          > Test.B = False
          > MsgBox(Test.A) ' 'False'.
          > MsgBox(Test.B) ' 'False'.
          > Dim t As New Test
          > MsgBox(Test.A) ' 'False'.
          > MsgBox(Test.B) ' 'False'.
          > .
          > .
          > .
          > Public Class Test
          > Public Shared A As Boolean
          > Public Shared B As Boolean = True
          >
          > Shared Sub New()
          > A = True
          > End Sub
          > End Class
          > ///
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B <URL:http://classicvb.org/petition/>[/color]


          Comment

          Working...