By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

VB.NET to C#

P: n/a

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

Nov 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
freddie <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
freddie <an*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

P: n/a
The "static" equivalent in VB.Net is "Shared".
Can we have a static field in the method in C# or
do we need to declare it outside. You need to declare it outside.
http://www.google.ca/groups?hl=en&lr...TNGP09.phx.gbl

--
Tim Wilson
..Net Compact Framework MVP

"freddie" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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

Nov 17 '05 #4

P: n/a
The "static" equivalent in VB.Net is "Shared".
Can we have a static field in the method in C# or
do we need to declare it outside. You need to declare it outside.
http://www.google.ca/groups?hl=en&lr...TNGP09.phx.gbl

--
Tim Wilson
..Net Compact Framework MVP

"freddie" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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

Nov 17 '05 #5

P: n/a
Sorry. Disregard the first comment in my reply. I read the first part of the
post wrong.

--
Tim Wilson
..Net Compact Framework MVP

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:u9*************@TK2MSFTNGP15.phx.gbl...
The "static" equivalent in VB.Net is "Shared".
Can we have a static field in the method in C# or
do we need to declare it outside. You need to declare it outside.

http://www.google.ca/groups?hl=en&lr...TNGP09.phx.gbl
--
Tim Wilson
.Net Compact Framework MVP

"freddie" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...

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


Nov 17 '05 #6

P: n/a
Sorry. Disregard the first comment in my reply. I read the first part of the
post wrong.

--
Tim Wilson
..Net Compact Framework MVP

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:u9*************@TK2MSFTNGP15.phx.gbl...
The "static" equivalent in VB.Net is "Shared".
Can we have a static field in the method in C# or
do we need to declare it outside. You need to declare it outside.

http://www.google.ca/groups?hl=en&lr...TNGP09.phx.gbl
--
Tim Wilson
.Net Compact Framework MVP

"freddie" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...

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


Nov 17 '05 #7

P: n/a

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#

>
> 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
>


.

Nov 17 '05 #8

P: n/a
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;
}

--
Tim Wilson
..Net Compact Framework MVP

"Fred" <an*******@discussions.microsoft.com> wrote in message
news:1b****************************@phx.gbl...

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#

>
> 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
>

.

Nov 17 '05 #9

P: n/a
On 2005-03-28, Tim Wilson <> wrote:
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.
Nov 17 '05 #10

P: n/a
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.

--
Tim Wilson
..Net Compact Framework MVP

"David" <df*****@woofix.local.dom> wrote in message
news:slrnd4e03m.5af.df*****@woofix.local.dom...
On 2005-03-28, Tim Wilson <> wrote:
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.

Nov 17 '05 #11

P: n/a
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
Nov 17 '05 #12

P: n/a

thanks guys, that cleared my doubts
-----Original Message-----
On 2005-03-28, Tim Wilson <> wrote:
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 scopeand delayed thread-safe initialization.
.

Nov 17 '05 #13

P: n/a
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

"David" wrote:
On 2005-03-28, Tim Wilson <> wrote:
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.

Nov 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.