469,364 Members | 2,346 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

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
Nov 21 '05 #1
4 4533
"Boni" <oilia@nospam> schrieb:
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
///

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

Nov 21 '05 #2
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]" <hi***************@gmx.at> schrieb im Newsbeitrag
news:%2***************@TK2MSFTNGP09.phx.gbl...
"Boni" <oilia@nospam> schrieb:
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
///

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

Nov 21 '05 #3
"Boni" <oilia@nospam> schrieb:
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
///

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

Nov 21 '05 #4
Thank you for your help
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb im Newsbeitrag
news:et**************@TK2MSFTNGP09.phx.gbl...
"Boni" <oilia@nospam> schrieb:
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
///

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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by John Kraft | last post: by
3 posts views Thread by Joe Fallon | last post: by
10 posts views Thread by darrel | last post: by
15 posts views Thread by Rob Nicholson | last post: by
2 posts views Thread by Phillip N Rounds | last post: by
4 posts views Thread by Bram Kuijper | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.