473,386 Members | 1,886 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,386 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 2249
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.