VB.NET to C#

F

freddie

Guys;

could someone please tell me the equivalent for the
Static keyword in C#

public function testFunc() as Integer

'----------------------------------------
' Is it possible to do this from inside
' the function in C#

Static Dim count as Integer = 0

count += 1

return count

end function


Can we have a static field in the method in C# or do we
need to declare it outside

Thanks

Fred
 
J

Jon Skeet [C# MVP]

freddie said:
could someone please tell me the equivalent for the
Static keyword in C#

public function testFunc() as Integer

'----------------------------------------
' Is it possible to do this from inside
' the function in C#

Static Dim count as Integer = 0

count += 1

return count

end function


Can we have a static field in the method in C# or do we
need to declare it outside

You need to declare it as a member of the type. If you think about it,
the value is part of the state of the object, so it makes sense to
declare it as such.
 
T

Tim Wilson

Sorry. Disregard the first comment in my reply. I read the first part of the
post wrong.
 
F

Fred

guys,

the static keyword in VB.NET can work from inside methods
i.e if you declare a variable as static in a method it is
equal to a shared variable in the class and for each
subsequent call to the method the value of count is
incremented from its previous value.(pls see code below)

is there an equivalent for this in C#
 
T

Tim Wilson

You'll need to declare a static field (or just a field, depending on what
you need it for) at the class level, and then you can increment the value in
the method.

private static int count = 0;

....

public int Increment()
{
count++;
return count;
}
 
D

David

You'll need to declare a static field (or just a field, depending on what
you need it for) at the class level, and then you can increment the value in
the method.

private static int count = 0;

...

public int Increment()
{
count++;
return count;
}

It's kinda weird, but a closer equivalent is actually:

private int count = 0;


Variables declared Static in VB.Net aren't actually, well, static.
There's one per instance. They're just normal fields with limited scope
and delayed thread-safe initialization.
 
T

Tim Wilson

Yeah, it seems odd. Actually, from the help.

"Static variables are locals that retain their value across invocations of
the method. Static variables declared within nonshared methods are per
instance: each instance of the type that contains the method has its own
copy of the static variable. Static variables declared within Shared methods
are per type; there is only one copy of the static variable for all
instances. While local variables are initialized to their type's default
value upon each entry into the method, static variables are only initialized
to their type's default value when the type or type instance is initialized.
Static variables may not be declared in structures."

So it appears that the "staticness" of a Static variable depends on the
"staticness" of the method in which it's declared. Of course, the latter
makes sense.
 
C

Cor Ligthert

Freddie,

A static value from a procedure in VB is actually a global value from the
class.

Because of that I have seen that everybody set mostly those values in VB
beneath the procedure header, can you in my opinion just set it in C# direct
above that to get almost the same effect.

I hope this helps,

Cor
 
F

Fred

thanks guys, that cleared my doubts
-----Original Message-----


It's kinda weird, but a closer equivalent is actually:

private int count = 0;


Variables declared Static in VB.Net aren't actually, well, static.
There's one per instance. They're just normal fields with limited scope
and delayed thread-safe initialization.


.
 
G

Guest

VB static locals within non-shared methods correspond to non-static class
variables. VB static locals within shared methods correspond to static class
variables. After inputting the following two VB methods into our Instant C#
VB.NET to C# converter:

public function testFunc() as Integer
Static count as Integer = 0
count += 1
return count
end function

public shared function testSharedFunc() as Integer
Static count as Integer = 0
count += 1
return count
end function

Instant C# produces:

//INSTANT C# NOTE: These were formerly VB static local variables:
private int testFunc_count = 0;
private static int testSharedFunc_count = 0;

public int testFunc()
{
//INSTANT C# NOTE: VB local static variable moved to class level
// Static count as Integer = 0
testFunc_count += 1;
return testFunc_count;
}

public static int testSharedFunc()
{
//INSTANT C# NOTE: VB local static variable moved to class level
// Static count as Integer = 0
testSharedFunc_count += 1;
return testSharedFunc_count;
}

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to
VB.NET converter
 

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