473,396 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4878
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nan Li | last post by:
Hello, I got stuck in a situation I think many of you must have already experienced. I want to have a simple lookup map inside my class. All the instances share that map. The code is as follows....
4
by: John Kraft | last post by:
Hi all, My question is more of a phylisophical one here, but I am wondering what the difference is (effectively and performance wise) between using a shared variable/static variable and using a...
3
by: Joe Fallon | last post by:
I have a Shared varibale in a base class and all the Shared methods in the sub-classes use it (and CHANGE it). I thought I was saving myself the "trouble" of Dimming this variable inside each...
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
10
by: ibiza | last post by:
Hi, I have a class which will be used as a utility class (all its functions shared, no need to instantiate it, let's say just to calculate things) It needs a constant dictionary(of char,...
2
by: Phillip N Rounds | last post by:
I'm working on a UserControl ( ASP.NET 1.1, C#, VS 2003), where I can't manage the state of multiple controls correctly. The controls have the following constraints 1: It has two possible...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.