Static in C#

  • Thread starter Thread starter Prateek
  • Start date Start date
P

Prateek

Is there a C# equivalent to static keyword in VB.NET? Or, some way to implement the exactly same functionality in C#?
 
Is there a C# equivalent to static keyword in VB.NET? Or, some way to implement the exactly same functionality in C#?

No, C# doesn't support static locals. Use a class instance field
instead.



Mattias
 
That is not true. The equivalant in VB is Shared. The difference is that VB
is not as strict and allows the member to be referenced through the instance
or through the class HOWEVER the member is still static within the class
(as per C#).
Visual Basic .NET
Public Class StatDemo
Public Shared aField As Integer
End ClassVisual C#
public class StatDemo
{
public static int aField;
}

--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
implement the exactly same functionality in C#?
 
Mark Broadbent said:
That is not true. The equivalant in VB is Shared. The difference is that VB
is not as strict and allows the member to be referenced through the instance
or through the class HOWEVER the member is still static within the class

That's not what was asked for though. What was asked for is the C#
equivalent of VB.NET's Static, which is used for local variables.
Mattias is absolutely right - there is no C# equivalent for VB.NET's
Static semantics.
 
I'll keep my mouth shut! Sorry all, I'll learn to read the posts properly
next time.
:-D
 
Mark Broadbent said:
I'll keep my mouth shut! Sorry all, I'll learn to read the posts properly
next time.

Oh I wouldn't say that at all - if I decided to shut up every time I
got something wrong, I'd never say anything at all :)

Sorry if I was a bit harsh in my previous post...
 
Thanks to all for your inputs...

Isn't there any way to implement this functionality.. suppose there is a
function and I want to know how many times it is called.. I do not want to
use a global variable or byref parameter.. I can do that easily in VB.NET
using static..
but in c#, how?


Mark Broadbent said:
I'll keep my mouth shut! Sorry all, I'll learn to read the posts properly
next time.

Oh I wouldn't say that at all - if I decided to shut up every time I
got something wrong, I'd never say anything at all :)

Sorry if I was a bit harsh in my previous post...
 
not at all m8, I've got very thick skin (some would say my head inherits
this class too!).
 
Prateek said:
Isn't there any way to implement this functionality.. suppose there is a
function and I want to know how many times it is called.. I do not want to
use a global variable or byref parameter.. I can do that easily in VB.NET
using static..
but in c#, how?

Use a static or instance variable - and if the method really *does*
have state which isn't appropriate to put in the class, then consider
encapsulating it in a separate class.
 
Jon said:
Use a static or instance variable - and if the method really *does*
have state which isn't appropriate to put in the class, then consider
encapsulating it in a separate class.

This is one thing I am not thrilled about C#... C had it, the CLI has
it, VB.NET exposes it, but C# does not. If I have a method that needs to
act differently when it's recursing vs. not, I'd hate to have to the
variable that indicates that outside the method body.
 
Kevin,
the CLI has it
Not that I am aware of!
VB.NET exposes it
VB.NET exposes it by adding two (hidden) instance fields to your class, plus
some interesting code in the method itself (for initialization). One field
is for the value itself, the second is used as a InitFlag for initialization
of the value itself (you only want it initialized once).

Look at the IL (below) using ILDASM.EXE of a VB.NET class & function that
has static field. You can use the same technique (mangled field names) in
your C# code to get the same effect.

However as the others have suggested, is this correct OOP programming?

Hope this helps
Jay

For example the following:

Public Sub TestStatic()
Static value As Integer = 100
value += 1
End Sub

Creates the following IL:

..field private specialname int32 $STATIC$TestStatic$2001$value

..field private specialname class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag $STATIC$TestStatic$2001$value$Init


..method public instance void TestStatic() cil managed
{
// Code size 131 (0x83)
.maxstack 3
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_0007: ldfld int16
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag::State
IL_000c: ldc.i4.1
IL_000d: beq.s IL_0072
IL_000f: ldarg.0
IL_0010: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_0015: call void
[mscorlib]System.Threading.Monitor::Enter(object)
IL_001a: nop
IL_001b: nop
.try
{
IL_001c: ldarg.0
IL_001d: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_0022: ldfld int16
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag::State
IL_0027: ldc.i4.0
IL_0028: bne.un.s IL_0040
IL_002a: ldarg.0
IL_002b: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_0030: ldc.i4.2
IL_0031: stfld int16
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag::State
IL_0036: ldarg.0
IL_0037: ldc.i4.s 100
IL_0039: stfld int32
PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value
IL_003e: br.s IL_0054
IL_0040: ldarg.0
IL_0041: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_0046: ldfld int16
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag::State
IL_004b: ldc.i4.2
IL_004c: bne.un.s IL_0054
IL_004e: newobj instance void
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IncompleteInit
ialization::.ctor()
IL_0053: throw
IL_0054: nop
IL_0055: leave.s IL_0071
} // end .try
finally
{
IL_0057: nop
IL_0058: ldarg.0
IL_0059: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_005e: ldc.i4.1
IL_005f: stfld int16
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag::State
IL_0064: ldarg.0
IL_0065: ldfld class
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StaticLocalIni
tFlag PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value$Init
IL_006a: call void
[mscorlib]System.Threading.Monitor::Exit(object)
IL_006f: nop
IL_0070: endfinally
} // end handler
IL_0071: nop
IL_0072: nop
IL_0073: ldarg.0
IL_0074: ldarg.0
IL_0075: ldfld int32
PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value
IL_007a: ldc.i4.1
IL_007b: add.ovf
IL_007c: stfld int32
PremiumAudit.BafidiForm::$STATIC$TestStatic$2001$value
IL_0081: nop
IL_0082: ret
} // end of method BafidiForm::TestStatic
 
Kevin P. Fleming said:
This is one thing I am not thrilled about C#... C had it, the CLI has
it

Like Jay, I don't believe this is true. Try running ildasm on a VB.NET
program which has a static local variable.
VB.NET exposes it, but C# does not. If I have a method that needs to
act differently when it's recursing vs. not, I'd hate to have to the
variable that indicates that outside the method body.

So pass the variable in as a parameter, by reference if necessary.
There are always ways round this kind of thing.
 
Jon said:
Like Jay, I don't believe this is true. Try running ildasm on a VB.NET
program which has a static local variable.

This was my error, I didn't know that VB.NET implemented it using such
an ugly method, I presumed that the CLI must have support for it. I was
wrong :-)
So pass the variable in as a parameter, by reference if necessary.
There are always ways round this kind of thing.

Yes, agreed that there are multiple ways to do the job. However, passing
it in as a parameter now means that the caller will know about it and
must pass a suitable "initial" value, or a second method that acts as a
wrapper for the recursing method must be created to hide this from
callers outside the type. I'm not saying this is terrible, it's just not
pretty and this idiom is used a great deal outside the C# world, so it's
unfortunate that the language does not implement a simple method to do it.
 
Kevin P. Fleming said:
Yes, agreed that there are multiple ways to do the job. However, passing
it in as a parameter now means that the caller will know about it and
must pass a suitable "initial" value, or a second method that acts as a
wrapper for the recursing method must be created to hide this from
callers outside the type. I'm not saying this is terrible, it's just not
pretty and this idiom is used a great deal outside the C# world, so it's
unfortunate that the language does not implement a simple method to do it.

No, I don't believe it is - because it's not an appropriate idiom in
OO, IMO. Just because it's used outside OO doesn't mean it should be
included in an OO language. The things which have state in OO are
objects and types - and modelling any "method with state" as a type
where you can have multiple instances etc is much more likely to end up
in a good abstraction, IME.
 
No, I don't believe it is - because it's not an appropriate idiom in
OO, IMO. Just because it's used outside OO doesn't mean it should be
included in an OO language. The things which have state in OO are

How in the world did local statics get the axe, but labels and "goto" made
it in? :)
 
Eric said:
How in the world did local statics get the axe, but labels and "goto" made
it in? :)

Labels for some things aren't too bad (they can be helpful in Java for
continuing the correct loop, etc) but I still avoid goto.

However, I think there *is* a (fairly fine) distinction between the
model that C# provides in terms of state, and the way that you actually
implement methods. The modelling side is more purist than the
implementation side, if you see what I mean. Or maybe I'm making it
up...
 
Ravichandran J.V. said:

No, that's the VB.NET equivalent of C#'s "static", not C#'s equivalent
of VB.NET's "Static". (C# doesn't *have* an equivalent of VB.NET's
"Static".)
 
Back
Top