473,581 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Methods

Hello, hoping you can clear up a little bit of confusion that I have on
creating/using static methods.

I want to create a class which hold all my "Utility Methods" for windows
forms. So I created a new project and added that project to my existing
project. The project "type" I selected was a "Class Library". What
this a correct choice.

So if I want to create "Static" methods, does the class itself have to
be static? There is no real reason to instatiate the class, so I am
guessing that I should make the class static. When I attempt to do
this, it tells me the modifier is not valid for this item. Have I got
the wrong format, or the wrong idea? Here is my code...

namespace WindowFormsUtil ities
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public static class WindFormUtiliti es
{
public static WindFormUtiliti es()
{
//
// TODO: Add constructor logic here
//
}
public static bool SetComboBoxSele ctionError(Comb oBox cbo,
ErrorProvider err)
{
}

private bool[] SetComboBoxSele ctionError (ComboBox[] cbo,
ErrorProvider err)
{
}

}
}
Nov 15 '05 #1
8 5491
Just make a regular class and make the methods (only) static. All data used
int her methods will also have to be declared static.
"Jim Heavey" <Ji*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.16...
Hello, hoping you can clear up a little bit of confusion that I have on
creating/using static methods.

I want to create a class which hold all my "Utility Methods" for windows
forms. So I created a new project and added that project to my existing
project. The project "type" I selected was a "Class Library". What
this a correct choice.

So if I want to create "Static" methods, does the class itself have to
be static? There is no real reason to instatiate the class, so I am
guessing that I should make the class static. When I attempt to do
this, it tells me the modifier is not valid for this item. Have I got
the wrong format, or the wrong idea? Here is my code...

namespace WindowFormsUtil ities
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public static class WindFormUtiliti es
{
public static WindFormUtiliti es()
{
//
// TODO: Add constructor logic here
//
}
public static bool SetComboBoxSele ctionError(Comb oBox cbo,
ErrorProvider err)
{
}

private bool[] SetComboBoxSele ctionError (ComboBox[] cbo,
ErrorProvider err)
{
}

}
}

Nov 15 '05 #2
A class Library project is the correct project type, and no, you can't
create a static class. The general rule if you are creating a class that
has all static methods is to make it sealed, since deriving from the class
wouldn't gain anything, and to declare a private default constructor so no
one can create an instance of you class.
"Jim Heavey" <Ji*******@nosp am.com> wrote in message
news:Xn******** *************** **********@207. 46.248.16...
Hello, hoping you can clear up a little bit of confusion that I have on
creating/using static methods.

I want to create a class which hold all my "Utility Methods" for windows
forms. So I created a new project and added that project to my existing
project. The project "type" I selected was a "Class Library". What
this a correct choice.

So if I want to create "Static" methods, does the class itself have to
be static? There is no real reason to instatiate the class, so I am
guessing that I should make the class static. When I attempt to do
this, it tells me the modifier is not valid for this item. Have I got
the wrong format, or the wrong idea? Here is my code...

namespace WindowFormsUtil ities
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public static class WindFormUtiliti es
{
public static WindFormUtiliti es()
{
//
// TODO: Add constructor logic here
//
}
public static bool SetComboBoxSele ctionError(Comb oBox cbo,
ErrorProvider err)
{
}

private bool[] SetComboBoxSele ctionError (ComboBox[] cbo,
ErrorProvider err)
{
}

}
}

Nov 15 '05 #3
Jim,
So if I want to create "Static" methods, does the class itself have to
be static? There is no real reason to instatiate the class, so I am
guessing that I should make the class static. When I attempt to do
this, it tells me the modifier is not valid for this item. Have I got
the wrong format, or the wrong idea? Here is my code...


Unless you're using the Whidbey version of C#, there's no such thing
as a static class. You can make it abstract or add a private
constructor to prevent it from being instantiated.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4
n!
> So if I want to create "Static" methods, does the class itself have to
be static? There is no real reason to instatiate the class, so I am
guessing that I should make the class static. When I attempt to do
this, it tells me the modifier is not valid for this item. Have I got
the wrong format, or the wrong idea? Here is my code...


I generally make classes with only static members, sealed with a private
constructor. Similar to:

public sealed class MyUtilityClass
{
private MyUtilityClass( ) // Prevent direct instantiation.
{
}
}

The 'sealed' property prevents someone from inheriting the class, the
private constructor prevents someone from creating an instance of the class
:) From reading your mail I think 'sealed' is basically the equivalent of
what you were expecting a 'static' class to be.

n!
Nov 15 '05 #5
n! <nf********@nom ailplease.com> wrote:
I generally make classes with only static members, sealed with a private
constructor. Similar to:

public sealed class MyUtilityClass
{
private MyUtilityClass( ) // Prevent direct instantiation.
{
}
}

The 'sealed' property prevents someone from inheriting the class, the
private constructor prevents someone from creating an instance of the class
:) From reading your mail I think 'sealed' is basically the equivalent of
what you were expecting a 'static' class to be.


There's no real need to make the class sealed as well as only having a
private constructor - just the fact that its only constructor is
private means that you can't inherit from it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Then why does the compiler permit this combination? Isnt this like havint a
string as a const and static? You cant because it makes no sense, just as
what you say , it makes no sense.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
n! <nf********@nom ailplease.com> wrote:
I generally make classes with only static members, sealed with a private
constructor. Similar to:

public sealed class MyUtilityClass
{
private MyUtilityClass( ) // Prevent direct instantiation.
{
}
}

The 'sealed' property prevents someone from inheriting the class, the
private constructor prevents someone from creating an instance of the class :) From reading your mail I think 'sealed' is basically the equivalent of what you were expecting a 'static' class to be.


There's no real need to make the class sealed as well as only having a
private constructor - just the fact that its only constructor is
private means that you can't inherit from it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
<di********@dis cussion.microso ft.com> wrote:
Then why does the compiler permit this combination? Isnt this like havint a
string as a const and static? You cant because it makes no sense, just as
what you say , it makes no sense.


I think there's a difference in the level of logical connection there.
It's not that there's anything *syntactically* redundant about there
being a sealed class with only a private constructor, it's only
*semantically* redundant.

It would be reasonable of the compiler to complain, but I don't think
it's particularly necessary for it to do so.

(There's also the fact that the two will end up in different IL - a
class with only private constructors is *effectively* sealed, it isn't
*actually* sealed.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
n!
> There's no real need to make the class sealed as well as only having a
private constructor - just the fact that its only constructor is
private means that you can't inherit from it.


That's true, though the private constructor is there to prevent creation of
a class instance rather than prevent inheritance (even though it effectively
does that job as well), and the compile error is more meaningful when trying
to inherit a sealed class rather than a class with private constructors. :)

n!
Nov 15 '05 #9

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

Similar topics

13
10695
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. For example if you have an Employee class rather then instantiating an instance you call a static method 'GetEmployees' and it returns a List of...
4
8010
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned in "Learning Python" by Mark Lutz and David Ascher. It seems like they are a relatively new feature... It seems to me that any truly OO programming...
3
1985
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one from Guido: "The new descriptor API makes it possible to add static methods and class methods. Static methods are easy to describe: they behave...
1
2194
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason...
8
52747
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind it. thanks, Steven
3
9738
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx mauzi
3
2088
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of...
8
2049
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks. Fernando
12
3563
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
9
5833
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context,...
0
7862
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7789
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...
0
8301
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7894
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6551
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5670
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...
0
5361
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...
0
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1132
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...

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.