|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
VB.NET to C#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 freddie <anonym***@discussions.microsoft.com> wrote:
Show quote > could someone please tell me the equivalent for the You need to declare it as a member of the type. If you think about it, > 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 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 The "static" equivalent in VB.Net is "Shared".
> Can we have a static field in the method in C# or You need to declare it outside.> do we need to declare it outside. http://www.google.ca/groups?hl=en&lr=&threadm=uKkehfAfDHA.2484%40TK2MSFTNGP09.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26selm%3DuKkehfAfDHA.2484%2540TK2MSFTNGP09.phx.gbl -- Show quoteTim Wilson ..Net Compact Framework MVP "freddie" <anonym***@discussions.microsoft.com> wrote in message news:00c801c53300$37b9fec0$a501280a@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 > 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 http://www.google.ca/groups?hl=en&lr=&threadm=uKkehfAfDHA.2484%40TK2MSFTNGP09.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26selm%3DuKkehfAfDHA.2484%2540TK2MSFTNGP09.phx.gblnews:u9JLSQwMFHA.576@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. > Show quote > > -- > Tim Wilson > .Net Compact Framework MVP > > "freddie" <anonym***@discussions.microsoft.com> wrote in message > news:00c801c53300$37b9fec0$a501280a@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 > > > > 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# Show quote >> > >> > 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 >> > >> >> > > >. > 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; } -- Show quoteTim Wilson ..Net Compact Framework MVP "Fred" <anonym***@discussions.microsoft.com> wrote in message news:1bcb01c53325$d7cefbc0$a401280a@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 > >> > > >> > >> > > > > > >. > > On 2005-03-28, Tim Wilson <> wrote:
> You'll need to declare a static field (or just a field, depending on what It's kinda weird, but a closer equivalent is actually:> 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; > } 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. 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. -- Show quoteTim Wilson ..Net Compact Framework MVP "David" <dfos***@woofix.local.dom> wrote in message news:slrnd4e03m.5af.dfoster@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. > > thanks guys, that cleared my doubts
>-----Original Message----- depending on what>On 2005-03-28, Tim Wilson <> wrote: >> You'll need to declare a static field (or just a field, >> you need it for) at the class level, and then you can increment the value inShow quote >> 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. > > >. > 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 Show quote "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. > > > 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 |
|||||||||||||||||||||||