how to initialize shared variable

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

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
 
Boni said:
how can I initialize shared variable?
i.e
class A
private shared B as boolean
end class

\\\
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
///
 
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
 
Boni said:
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!!!

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
///
 
Thank you for your help
Herfried K. Wagner said:
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
///
 
Back
Top