473,804 Members | 3,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1655
> 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****@discuss ions.microsoft. com> schrieb im Newsbeitrag
news:15******** *************** ***********@mic rosoft.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***@sterck en-sorrenti.nospam .com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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****@discuss ions.microsoft. com> wrote in message
news:15******** *************** ***********@mic rosoft.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(Fooba r.Add(1,2));
=============== =======

and it is 'roughly the equivalent of:
=============== =======
Foobar foobar = new Foobar(1);
System.Console. WriteLine(fooba r.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(Fooba r.Add(2));
System.Console. WriteLine(Fooba r.Add(2));
System.Console. WriteLine(Fooba r.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
5905
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 example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
1
1725
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 who generate this schema. for any help Thanks ======== My xml file ================================= <ROOT> <Customers> <CustomerId>5555</CustomerId>
140
7921
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
2475
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 themselves, or should i return a code to main and handle the error there? If i don't return them to main, except for the structure, what use is the main function except for calling functions?
3
1293
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
1845
by: Umesh | last post by:
What is the use by making function Static.
669
26255
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
10
2113
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 statements into comments (placed an ' in front)
4
5314
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
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.