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

novice advice on static use

Hi

I am learning C# from books. I am trying to understand the use of the word
'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug
Sep 1 '06 #1
7 1546
A static item is bound to the type as opposed to the instance.

A great example would be ...

public class Foo {
public static int Bar;
public int Bar2;
}

Foo f = new Foo();
Foo f1 = new Foo();

f.Bar = 22;
f.Bar2 = 22;
f1.Bar = 33;
f.Bar2 = 33;

f.Bar is 33
f.Bar2 is 22
f1.Bar is 33
f1.Bar2 is 33

Sothe variable is shared amoungst all instances of the class ..

for methods they are called differently ..

public class Foo {
public static void Bar() {
Cons0ole.WriteLine("Bar");'
}
public static void Bar2() {
Cons0ole.WriteLine("Bar");'
}
}

I can call ...

Foo.Bar() (note I am using the typename)

If I were to try
Foo.Bar2() I would get an error (it is an instance member and needs to be
called from an instance such as)

Foo f = new Foo();
f.Bar2();

Does this make sense?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug

Sep 1 '06 #2
Sorry I meant to make properties in the first Foo class (one hitting a
static one hitting an instance variable).

Cheers,

Greg
"Greg Young" <dr*******************@hotmail.comwrote in message
news:um**************@TK2MSFTNGP05.phx.gbl...
>A static item is bound to the type as opposed to the instance.

A great example would be ...

public class Foo {
public static int Bar;
public int Bar2;
}

Foo f = new Foo();
Foo f1 = new Foo();

f.Bar = 22;
f.Bar2 = 22;
f1.Bar = 33;
f.Bar2 = 33;

f.Bar is 33
f.Bar2 is 22
f1.Bar is 33
f1.Bar2 is 33

Sothe variable is shared amoungst all instances of the class ..

for methods they are called differently ..

public class Foo {
public static void Bar() {
Cons0ole.WriteLine("Bar");'
}
public static void Bar2() {
Cons0ole.WriteLine("Bar");'
}
}

I can call ...

Foo.Bar() (note I am using the typename)

If I were to try
Foo.Bar2() I would get an error (it is an instance member and needs to be
called from an instance such as)

Foo f = new Foo();
f.Bar2();

Does this make sense?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
>Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public
members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug


Sep 1 '06 #3
*"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
* Hi
*
* I am learning C# from books. I am trying to understand the use of the
word
* 'Static' on a method.
*
* When i look in the autos area (I often like to see what values are being
* resolved) i see that there are also static members and non public members.
*
* Could you please explain to me how the static method differs from others,
* how static members differ from others?
*
* Some text and an example would be great, but a link to a novice focuseed
* website would be good to.
*
* Thanks in advance
*
* Doug
*
*

Doug,

Another quick answer: With static methods, you don't have to create an
object to use its static methods.
What does this mean? Suppose we have a Math class that returns the square
root of a number.
If it were not a static method (therefore an instance method),
you'd have to create a Math object (using the new operator)
and then call the square root method using the reference to your newly
created Math object.
Something like,
Math m = new Math( ); // Create new Math object
int i = m.SquareRoot(16); // Call the SquareRoot method from our newly
created Math object, m.

now, if the method is declared static, you don't need to create an object of
the Math class.
You can just use the method, but you need to let the compiler know the
method is in the Math class.
So you put the class name followed by the dot operator (.) and then the
static method name.
Something like,
int i = Math.SquareRoot(16);

This works for constants and whatnot as well, not just methods.
Perhaps, some common Math constants like pi, e, and whatnot would be
available to you w/o
needing to create a Math object first before having access to them.
A silly example:
double myNumber = Math.Pi + Math.e ;
-MH




Sep 1 '06 #4
Gordon,

Another short answer.

A static member is (in Microsoft Systems) always non destructable static
placed in the main stack of a program.

A non static member has to be instanced and is placed somewhere else and can
be created as much times as you want.

In other words, using only static classes means the same as non OOP
programming (more modulair programming).

Cor

"gordon" <go**********@optusnet.com.auschreef in bericht
news:44***********************@news.optusnet.com.a u...
Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug

Sep 2 '06 #5
Thanks Greg, Michael and Cor - your advice and assistance is appreciated.

Doug

"gordon" <go**********@optusnet.com.auwrote in message
news:44***********************@news.optusnet.com.a u...
Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug

Sep 2 '06 #6
Cor,

See inline:
A static member is (in Microsoft Systems) always non destructable static
placed in the main stack of a program.
It is not in the stack. The instance of the static type which holds the
fields lives on the heap.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>
"gordon" <go**********@optusnet.com.auschreef in bericht
news:44***********************@news.optusnet.com.a u...
>Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public
members.

Could you please explain to me how the static method differs from others,
how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug


Sep 5 '06 #7
Nicholas if I remember correctly it gets treated specially and lives in the
LOH since it is known to have a long lifespan.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u$**************@TK2MSFTNGP05.phx.gbl...
Cor,

See inline:
>A static member is (in Microsoft Systems) always non destructable static
placed in the main stack of a program.

It is not in the stack. The instance of the static type which holds
the fields lives on the heap.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>>
"gordon" <go**********@optusnet.com.auschreef in bericht
news:44***********************@news.optusnet.com. au...
>>Hi

I am learning C# from books. I am trying to understand the use of the
word 'Static' on a method.

When i look in the autos area (I often like to see what values are being
resolved) i see that there are also static members and non public
members.

Could you please explain to me how the static method differs from
others, how static members differ from others?

Some text and an example would be great, but a link to a novice focuseed
website would be good to.

Thanks in advance

Doug



Sep 5 '06 #8

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

Similar topics

7
by: gilgantic | last post by:
How do you navigate between screens in J2ME? I would like to navigate to the next screen using the default emulator in KToolbar. Here is a snippit of my commandAction method: public void...
21
by: AES/newspost | last post by:
My understanding -- I'm not an expert -- is that on (some? many? all?) standard Internet servers a URL can point to a subdirectory name followed by a backslash, and that links to this URL will...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
2
by: bole2cant | last post by:
I just received my free VB.NET package from MS, and can't figure out how to Save As. I loaded a sample program and made a bunch of changes to it and wanted to save it without overwriting the...
4
by: Ben Fidge | last post by:
My application uses a singleton static class for writing entries to a log file. The location and name of the log-file is read from web.config each time an entry is written, but has the current date...
25
by: gordon | last post by:
I aksed a few days ago about static methods and got some good answers that were really useful. Now I am not sure if about the use of static on members (variables) and classes. Can someone...
0
by: waggledance | last post by:
I was wondering if anyone here might be able to offer me some advice for someone who can only use Macromedia Dreamweaver MX. I am (clearly!) a web design novice so apologies in advance if this is a...
2
by: Hans Artm | last post by:
Hi, We are developing a site that needs to be localized (english + spanish). The site has a lot of "static" pages with a lot of text, and some dynamic pages. We are unsure how to develop the...
9
by: Mo | last post by:
After a little PHP education, my first project (to get my feet wet) is making an employee time-tracking program for our small business. *** ANY SUGGESTION, THOUGHTS, OR ADVICE WOULD BE WARMLY...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.