what's the equivalent

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,
vb has a static keyword for variables, what is charp's equivalent,please?

static tempVar as String

thanks,
rodchar
 
rodchar said:
hey all,
vb has a static keyword for variables, what is charp's equivalent,please?

static tempVar as String

also static in C#

as in:

static string tempVar;

Arne
 
The classic VB "static" keyword and the C# static (and VB.NET "Shared")
modifiers are not the same thing. In classic VB, "static" was used to retain
the value of a variable inside a function.

Shared and Static in VB.NET and C# respectively mean, when applied to a
variable, that all instances of the object share the same piece of data.

Peter
 
Peter said:
The classic VB "static" keyword and the C# static (and VB.NET "Shared")
modifiers are not the same thing. In classic VB, "static" was used to retain
the value of a variable inside a function.

Like C ?

What about simply moving it outside the function (and make
it static if it is a static method) ?

Arne
 
The "static" local variable in VB is not limited to "classic" VB, but is
alive and well in VB.NET also.
There is no C# equivalent. It can be replaced by a class-level instance or
static variable in C# (depending on whether the original method was an
instance or static method), but this doesn't quite capture the usefulness of
the local static variable.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
David Anton said:
The "static" local variable in VB is not limited to "classic" VB, but is
alive and well in VB.NET also.
There is no C# equivalent. It can be replaced by a class-level instance or
static variable in C# (depending on whether the original method was an
instance or static method), but this doesn't quite capture the usefulness of
the local static variable.

It does, however, *exactly* capture the semantics - as that's what the
VB.NET compiler converts the code into.

Personally I'm glad that C# doesn't have statics. Persistent state
should be a property of types and objects, not methods. Whenever a
method feels like it needs state, consider whether that state is
inherently part of the object, or whether it should be part of a new
class in its own right, which the object has a reference to an instance
of.
 
Arne Vajhøj said:
Like C ?

What about simply moving it outside the function (and make
it static if it is a static method) ?

No if. C/C++ local statics are reused between all invocations, independent
of arguments (the this pointer is really an argument). So you can use a
global variable or a class static to get the same result.
 
Jon Skeet said:
It does, however, *exactly* capture the semantics - as that's what the
VB.NET compiler converts the code into.

Personally I'm glad that C# doesn't have statics. Persistent state
should be a property of types and objects, not methods. Whenever a
method feels like it needs state, consider whether that state is
inherently part of the object, or whether it should be part of a new
class in its own right, which the object has a reference to an instance
of.

Agreed. While in C/C++ local statics have a use, namely caching the result
of a complex calculation/database lookup/object instantiation/etc., the VB
semantics are different, introducing *instance* variables. That's just
wrong.

Really, static should never be used to change the meaning of the program,
only to cache the result of an initialization which would take the same
value every time anyway.
 
The class level variables simulate the functionality (our converters do
that), but I wouldn't say they capture the semantics well, since the
semantics of "local static" mean that the variable is off limits to other
methods.
I think your additional object idea does capture the semantics very well
(i.e., a method needing state is refactored to a new class where the local
static becomes an object property). We would incorporate that in our
converters, but it's a little too convoluted (it's easier for people to
accept the class variable conversion than a refactoring of their classes).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
David,

Exact, I find it always amazing how some C# users are always bloating that
it is something wrong.

I have seen that it can be a very useful feature.

Moreover I have as well seen that the average VB.Net programmers is more
trying to avoid Global variables than C# ones, with the result that the GC
can do its job better with those VB.Net developers. (In this case from the
static keyword it is opposite, that is why I am always very careful when I
use or advice the static keyword in VB.Net, however it fulfils the behaviour
of the average VB.Net developer to declare variables as late as possible).

Just something I mentioned, and is proven in this thread were is by more
people told to set things global. I would like that C# had better (and
easier) possibilities as in VB.Net to declare things in a method.

Cor
 

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

Similar Threads

Truncate String 2
static and base classes 2
static vs session 2
hadn't seen 'using' here before 4
alternative way to validate 4
vb equivalent 2
property inference 5
vb equivalent in cs 6

Back
Top