Static Variables

R

Raghu

A local variable in a procedure can be declared as Static so that it can
retain value after the procedure ends. My question is: Does the variable
retain value among multiple objects of same type? Or is it specific to one
object instance?

Thanks.
 
N

Nick Malik [Microsoft]

all objects of the same class share the static values. The static value is
a property of the class itself, not of any instance of the class.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
J

J L

Hi Nick,
Does that mean that Static variables are shared..i.e. can be set
without creating an instance of the class?

TIA
John
 
N

Nick Malik [Microsoft]

yes

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
C

Cor Ligthert

JL,

Does that mean that Static variables are shared..i.e. can be set
without creating an instance of the class?

No

(The static keyword is often mixed up because the Static keyword in C# is
the same as the Shared keyword in VBNet. The reason from those different
names, is probably because there exist in the VB language a "Static"
Variable what is a special keyword).

Where the answer in Nick's first answer stays the same by the way.

Reading your question in a little different way. "Does that mean that Shared
variables are static....", than the answer on your last question would be
Yes, crazy enough.

I have tried to make it clear using this sample for you.

\\\
Public Class Class1
Public Shared Sub main()
Class1A.a = 1
Class1A.b = 1 'This is not possible
End Sub
End Class
Public Class Class1A
Public Shared a As Integer = 0
Public Shared Sub main()
Static b As Integer = 0
Static Shared c As Integer = 0 'This is not allowed
End Sub
End Class
///

I hope this helps?

Cor
 
N

Nick Malik [Microsoft]

good catch, Cor.

I'm a C# person. I made the same mistake.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
H

Herfried K. Wagner [MVP]

Raghu said:
A local variable in a procedure can be declared as Static so that it can
retain value after the procedure ends. My question is: Does the variable
retain value among multiple objects of same type? Or is it specific to one
object instance?


If the method they are declared in is not a shared method, they will belong
to the instances.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top