473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

VB.NET to C#


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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.