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 WindowFormsUtilities
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public static class WindFormUtilities
{
public static WindFormUtilities()
{
//
// TODO: Add constructor logic here
//
}
public static bool SetComboBoxSelectionError(ComboBox cbo,
ErrorProvider err)
{
}
private bool[] SetComboBoxSelectionError (ComboBox[] cbo,
ErrorProvider err)
{
}
}
} 8 5465
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*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.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 WindowFormsUtilities { /// <summary> /// Summary description for Class1. /// </summary> public static class WindFormUtilities { public static WindFormUtilities() { // // TODO: Add constructor logic here // } public static bool SetComboBoxSelectionError(ComboBox cbo, ErrorProvider err) { }
private bool[] SetComboBoxSelectionError (ComboBox[] cbo, ErrorProvider err) { }
} }
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*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.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 WindowFormsUtilities { /// <summary> /// Summary description for Class1. /// </summary> public static class WindFormUtilities { public static WindFormUtilities() { // // TODO: Add constructor logic here // } public static bool SetComboBoxSelectionError(ComboBox cbo, ErrorProvider err) { }
private bool[] SetComboBoxSelectionError (ComboBox[] cbo, ErrorProvider err) { }
} }
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.
> 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!
n! <nf********@nomailplease.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.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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.com> wrote in message
news:MP************************@msnews.microsoft.c om... n! <nf********@nomailplease.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.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
<di********@discussion.microsoft.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.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
> 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. ...
|
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...
|
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...
|
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...
|
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...
| |
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
...
|
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...
|
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....
|
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
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |