Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2008

Tip: Looking for answers? Try searching our database.

Question about 'public' and 'public static'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Schizoid Man - 01 Mar 2008 15:03 GMT
Why do I have to declare a 'public' function as 'static' in a public
class in order to make it visible from another class?

In this regard I don't understand how public functions without the
static declaration are different from private functions, because I
cannot call these method from outside the class that those methods are
declared in.

All classes refer to the same namespace.

Thanks,
Schiz
Jon Skeet [C# MVP] - 01 Mar 2008 15:25 GMT
> Why do I have to declare a 'public' function as 'static' in a public
> class in order to make it visible from another class?
[quoted text clipped - 5 lines]
>
> All classes refer to the same namespace.

Whether a member (method, property, variable etc) is static or not has
nothing to do with whether or not it's public. It's to do with whether
it's conceptually something to do with the type as a whole, or an
individual instance of the type.

It's not a hugely easy topic to describe quickly in a newsgroup post,
but I suggest you consult a book or tutorial about it - it's a key
concept in .NET.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Peter Morris - 01 Mar 2008 17:00 GMT
> Why do I have to declare a 'public' function as 'static' in a public class
> in order to make it visible from another class?

You don't, but you do have to create an instance of the class to see the
public method.

public class MyPublicClass
{
   public static void PublicStaticMethod() { }
   public void PublicInstanceMethod() { }
}

in another class.....

MyPublicClass. (code insight will only show PublicStaticMethod in the list)

MyPublicClass instance = new MyPublicClass();
instance. (code insight will show PublicInstanceMethod)

Pete
Bob Powell [MVP] - 01 Mar 2008 21:08 GMT
You seem to have confused the intent of the public versus private accessor
and the static keyword.

Static defines a method, property or class that is associated with a
particular type but which does not require access to the instance members of
the type. A static method can be called using the form
MyType.MyStaticMethod. The static method must be public in order to be used
from another class.

A non static public method requires an instance of the class:

MyType mt=new MyType();
mt.MyPublicMethod();

A static method may not access any instance data or other instance methods
of a class so this will not work:

class MyClass
{
   int t;

   public static void ChangeT()
   {
       t=10;
   }
}

I hope this clarifies somewhat but as John suggests you should probably be
looking at a textbook.
Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Why do I have to declare a 'public' function as 'static' in a public class
> in order to make it visible from another class?
[quoted text clipped - 7 lines]
> Thanks,
> Schiz
Schizoid Man - 02 Mar 2008 11:35 GMT
> You seem to have confused the intent of the public versus private
> accessor and the static keyword.
[quoted text clipped - 25 lines]
> I hope this clarifies somewhat but as John suggests you should probably
> be looking at a textbook.

Thank you all for the replies - I really appreciate it. I have a C++
background  and am preparing for an interview.

Suppose I am writing a general utility 'class' (simple functions like
Max, Min, Average, etc) that does not need access to instance members
because each function is self contained.

Wouldn't it be inefficient for me to instantiate the Utility class and
then class the appropriate Utility.Method rather than applying the
method straight away without the creating the instance?

I could achieve this in C++ by declaring the methods outside of a class
in the header and then include the appropriate header in the source file.

What would be the equivalent in C#? Interpreting Bob's description,
declaring the appropriate methods as static would be the way to go.

Thanks again.
Bob Powell [MVP] - 02 Mar 2008 13:37 GMT
The case you cite this is a valid use of the static method. System.Math is a
prime example of this. Each method is self contained, works on the
parameters supplied and returns some value. System.Math cannot be
instantiated and encapsulates no data.

Take a look at the method prototypes for Math or perhaps peek inside with
Reflector.

Be careful when designing your architecture that you don't begin using the
static system as a substitute for global methods and variables. This is very
bad pracatice.

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

>> You seem to have confused the intent of the public versus private
>> accessor and the static keyword.
[quoted text clipped - 44 lines]
>
> Thanks again.
Jon Skeet [C# MVP] - 02 Mar 2008 13:48 GMT
> Thank you all for the replies - I really appreciate it. I have a C++
> background  and am preparing for an interview.
>
> Suppose I am writing a general utility 'class' (simple functions like
> Max, Min, Average, etc) that does not need access to instance members
> because each function is self contained.

Right. In that case, not only could all the methods be static, but you
might want to mark the whole *class* as being static. That would
prevent it being instantiated in the first place.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Schizoid Man - 02 Mar 2008 19:15 GMT
>> Thank you all for the replies - I really appreciate it. I have a C++
>> background  and am preparing for an interview.
[quoted text clipped - 6 lines]
> might want to mark the whole *class* as being static. That would
> prevent it being instantiated in the first place.

Thank you for the answers.
Schizoid Man - 03 Mar 2008 16:26 GMT
>> Thank you all for the replies - I really appreciate it. I have a C++
>> background  and am preparing for an interview.
[quoted text clipped - 6 lines]
> might want to mark the whole *class* as being static. That would
> prevent it being instantiated in the first place.

Hi Jon, Bob,

Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

Regards,
Schiz
Schizoid Man - 03 Mar 2008 16:26 GMT
>> Thank you all for the replies - I really appreciate it. I have a C++
>> background  and am preparing for an interview.
[quoted text clipped - 6 lines]
> might want to mark the whole *class* as being static. That would
> prevent it being instantiated in the first place.

Hi Jon, Bob,

Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

Regards,
Schiz
Schizoid Man - 03 Mar 2008 16:28 GMT
>> Thank you all for the replies - I really appreciate it. I have a C++
>> background  and am preparing for an interview.
[quoted text clipped - 6 lines]
> might want to mark the whole *class* as being static. That would
> prevent it being instantiated in the first place.

Hi Jon, Bob,

Just to continue this thread for a moment...

Suppose I'm writing a class for a statistical distribution. I write the
appropriate constructors, properties and methods (in this case a simple
mean, or average). In this case I would just use the declaration 'public
double', and its usage would be:

StatDist test = new StatDist();
double Avg = test.Average();

However, if I want to also write a general purpose Average function in
the same class, I'd want to overload the function using 'public static
double' so that its usage would be:

double x[] = { ... };
double Avg = StatDist.Average(x);

Is it a good (or bad) idea to overload the function and make one of them
static all in the same class?

Regards,
Schiz
Jon Skeet [C# MVP] - 03 Mar 2008 16:37 GMT
> Just to continue this thread for a moment...
>
[quoted text clipped - 15 lines]
> Is it a good (or bad) idea to overload the function and make one of them
> static all in the same class?

I think it's reasonable in some cases. If you have a look at
Regex.Replace, it has exactly that sort of thing - various overloads,
some of which are static method and take a pattern, and some of which
are instance methods which use the target regular expression for the
pattern.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Schizoid Man - 03 Mar 2008 22:48 GMT
>> Just to continue this thread for a moment...
>>
[quoted text clipped - 21 lines]
> are instance methods which use the target regular expression for the
> pattern.

Thank you.
Ben Voigt [C++ MVP] - 03 Mar 2008 23:16 GMT
> Thank you all for the replies - I really appreciate it. I have a C++
> background  and am preparing for an interview.
[quoted text clipped - 14 lines]
>
> Thanks again.

C++ uses static in *exactly* the same way.  From the Visual C++ Language
Reference:

"When modifying a data member in a class declaration, the static keyword
specifies that one copy of the member is shared by all instances of the
class. When modifying a member function in a class declaration, the static
keyword specifies that the function accesses only static members."

Of course this is highly inaccurate.  Static member functions can access
non-static members, both functions and data, as long as an instance of the
class is referenced in the process.  Since the static member function is a
member of the class, it can access private and protected members in this way
as well.
Baktiar Khan - 03 Jun 2008 16:00 GMT
I want to learn visual C#2005.NET

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.