473,321 Members | 1,877 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,321 software developers and data experts.

what static fuction mean in c#

when i was usong c++ idont have to put static before tha function that i want
to acces but in c# i have to but it .
ex
:
ihave a function called add(int x,int y)
i have to put
static int add(int x,int y) in prototype
or i have this
"an object refrence is required for the nonstatic field, methiod or property"

i hope someone to tell me why as i think the idea of static in c# is not
like in c++
or tell me where to post this

thanx at all

Nov 17 '05 #1
5 1635
> when i was usong c++ idont have to put static before tha function that i
want
to acces but in c# i have to but it .
Well, in both languages you have to declare a member function as static if
you want to call that function without an instance of the class - it's the
same in both languages. In C++, like in C#, you need an instance to call a
nonstatic member function. I don't see why you think this would be
different?

--
Guido Stercken-Sorrenti
MVP - Visual Developer / Visual C++
"SemSem" <Se****@discussions.microsoft.com> schrieb im Newsbeitrag
news:15**********************************@microsof t.com... ihave a function called add(int x,int y)
i have to put
static int add(int x,int y) in prototype
or i have this
"an object refrence is required for the nonstatic field, methiod or
property"

i hope someone to tell me why as i think the idea of static in c# is not
like in c++
or tell me where to post this

thanx at all

Nov 17 '05 #2

"Guido Stercken-Sorrenti [MVP VC++]" <ms***@stercken-sorrenti.nospam.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
when i was usong c++ idont have to put static before tha function that i want
to acces but in c# i have to but it .


Well, in both languages you have to declare a member function as static if you want to call that
function without an instance of the class - it's the same in both languages. In C++, like in C#,
you need an instance to call a nonstatic member function. I don't see why you think this would be
different?


The difference is that in C++ you can declare functions outside of any class. (like in C)
In C# all functions/methods must be part of a Class.
If you don't declare the method as static it means that it is associated with a particular OBJECT.
If you do declare the method as static it means that it is associated with the CLASS.

Bill
Nov 17 '05 #3
hi,

yesterday there was a thread about this, named "static methods" it may be of
help
cheers,


"SemSem" <Se****@discussions.microsoft.com> wrote in message
news:15**********************************@microsof t.com...
when i was usong c++ idont have to put static before tha function that i
want
to acces but in c# i have to but it .
ex
:
ihave a function called add(int x,int y)
i have to put
static int add(int x,int y) in prototype
or i have this
"an object refrence is required for the nonstatic field, methiod or
property"

i hope someone to tell me why as i think the idea of static in c# is not
like in c++
or tell me where to post this

thanx at all

Nov 17 '05 #4


"Guido Stercken-Sorrenti [MVP VC++]" wrote:
when i was usong c++ idont have to put static before tha function that i
want
to acces but in c# i have to but it .


Well, in both languages you have to declare a member function as static if
you want to call that function without an instance of the class - it's the
same in both languages. In C++, like in C#, you need an instance to call a
nonstatic member function. I don't see why you think this would be
different?

iam not talking about member function in class
in c++ i prototype the function and call it without give me that error
maybe as bill say because of every main have to be in class .
amd i see this because i try it!
Nov 17 '05 #5
First, in c# every there is no soch thing as a function. there are methods.

Every method must be a member of a class -

for example:
======================
namespace foo
{
int Add(int x,int y){ return x+y;}
}
======================

will not compile because, in C# "A namespace does not directly contain
members such as fields or methods"

but this does:
======================
namespace foo
{
class Foobar
{
int x = 0;
public Foobar(int n){ x = n;}
public int Add(int y){ return x+y;}
}
}
======================

As the 'Add' method is a member of the class Foobar which is 'directly
contianed' in the foo namespace. But to use it, you need an instance of
Foobar:

for example:
======================
Foobar foobar;
foobar.Add(4);
======================
won't compile: "unassigned local variable 'foobar'"
Now, with regards to static -

A method modified by the keywords 'static' in C# means that you can call the
method without instancing the containing class.

Lets modify the foobar class above:

======================
namespace foo
{
class Foobar
{
int x = 0;
public Foobar(int n){ x = n;}
public int Add(int y){ return x+y;}
public static int Add(int x, int y){ return x+y;}
}
}
======================

this line will work without an actual instance of Foobar:
======================
System.Console.WriteLine(Foobar.Add(1,2));
======================

and it is 'roughly the equivalent of:
======================
Foobar foobar = new Foobar(1);
System.Console.WriteLine(foobar.Add(2));
======================

Now, static methods cannot reference non-static members, or 'Instance
Members' of their containing class.

for example:
======================
namespace foo
{
class Foobar
{
int x = 0;
public static int Add(int y){return x+=y;}
}
}
======================

does not compile: "An object reference is required for the nonstatic field,
method, or property 'foo.Foobar.x'"

x is only available within actual instances of a foobar class where the
static Add method does not belong to the instance but the class definition.

But, you can mark x as static and it becomes available:
======================
namespace foo
{
class Foobar
{
static int x = 0;
public static int Add(int y){return x+=y;}
}
}
======================

The following does compile as x is declared a static member of Foobar. But
tis has a side effect. x belongs now to ALL instances of Foobar changing one
x, changes x through out the application:

======================
System.Console.WriteLine(Foobar.Add(2));
System.Console.WriteLine(Foobar.Add(2));
System.Console.WriteLine(Foobar.Add(4));
======================

outputs:
======================
2
4
8
======================
as the static x is modified on each call.

make sense????
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
1
by: Szaki | last post by:
I use a BulkLoad to import file.xml to my base MS Server 2000. To import this xml file I need schema file. Mayby you know how to do this file mechanicy f.g. mayby somebody have some script in .net...
140
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
3
by: mike | last post by:
i've already used static fuction to make website i wanna common module that's why i used static fuctions. may be it's more than 100 fuction ...is it ok?
3
by: Umesh | last post by:
What is the use by making function Static.
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
4
by: dolphin | last post by:
Hi All I read a .cpp files,find that static void fun(void){......} int main() { .......... } What does this static function mean?Is it the same as the static
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll 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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.