473,396 Members | 1,827 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.

Question about "static class"

I'm trying to understand what defining a class as 'static' does for
me.

Here's an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the 'static' keyword held my salvation somehow. I'd like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.
Nov 16 '05 #1
9 2281
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com...
I'm trying to understand what defining a class as 'static' does for
me.

Here's an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the 'static' keyword held my salvation somehow. I'd like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.

Nov 16 '05 #2

"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing
else if you choose.


Technically there is, if the OP was using C# 2(since the express product
betas came out, the possiblity is there). Static classes in C# 2 are simply
classes that can only have static members and that automate the sealing
pattern you ahve to use for a pure static class.

However, in that edge case, the rest of the answer is perfectly valid, thats
just a technicality to avoid confusion(And probably create some, ;).

Nov 16 '05 #3
The poster may be talking about Visual Studio 2005 (Whidbey) which introduces the concept of a static class - one that can only have static members and cannot be instantiated.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#X**************@TK2MSFTNGP15.phx.gbl>

There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com...
I'm trying to understand what defining a class as 'static' does for
me.

Here's an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the 'static' keyword held my salvation somehow. I'd like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #4
Thank you very much. This helps to straighten me out.

One problem I see is that if I create the class with static members, I
have now realized that if the user changes their font selection that I
will need to have them exit and re-start the application in order for
the changes to take effect.

Is there no other mechanism I can use? My fundamental goal is to
create something that is globally available to all objects without
each of them having to create their own instance of it. But I want to
be able to change the values held by this object/class during runtime.

I have this sense that I'm sitting on a brain fart and that the
solution is known to me. Any assistance is greatly appreciated.

-Neil K.
On Sat, 2 Oct 2004 07:25:57 -0700, "Bob Grommes" <bo*@bobgrommes.com>
wrote:
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com.. .
I'm trying to understand what defining a class as 'static' does for
me.

Here's an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the 'static' keyword held my salvation somehow. I'd like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.


Nov 16 '05 #5
Wait, I may have answered my own question. I have been thinking of
'static' much the way I do on 'const'. I was thinking that the value
held by a 'static' variable could not be changed. But, if I'm
thinking correctly now, I believe that the limiktation of a 'static'
value is that it can not be destroyed and re-created (I'm sure I'm
using the wrong terms here). So, I can initialize the static font
object using the constructor, as I am prone to do. I can't do that
again on that static object, but I can change the font size property
of that object.

Well, I'll go run a test and I'll know. I just wanted to state that I
believe I have been looking at this problem all wrong.

Thanks,
-Neil K.
On Sat, 02 Oct 2004 17:50:32 -0500, Neil Kiser
<neil@NO_SPAM_PLEASEkiserville.com> wrote:
Thank you very much. This helps to straighten me out.

One problem I see is that if I create the class with static members, I
have now realized that if the user changes their font selection that I
will need to have them exit and re-start the application in order for
the changes to take effect.

Is there no other mechanism I can use? My fundamental goal is to
create something that is globally available to all objects without
each of them having to create their own instance of it. But I want to
be able to change the values held by this object/class during runtime.

I have this sense that I'm sitting on a brain fart and that the
solution is known to me. Any assistance is greatly appreciated.

-Neil K.
On Sat, 2 Oct 2004 07:25:57 -0700, "Bob Grommes" <bo*@bobgrommes.com>
wrote:
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com. ..
I'm trying to understand what defining a class as 'static' does for
me.

Here's an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the 'static' keyword held my salvation somehow. I'd like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.


Nov 16 '05 #6
Actually, no. You can expose a static method that can be called to rebuild
the font info, or you can expose static members that can be changed from
outside. "Static" is not meant as "unchanging". I think it was probably a
term borrowed from the idea of static variables in procedural languages --
static variables are variables that keep their values between calls to a
procedure. Such variables are "static" in the sense that they don't get
deallocated or cleared when the routine exits.

If it helps you (and it pains me to say it in a C# group) you can think of
VB.NET's equivalent keyword, "Shared", which I think is more intuitive than
static. Static values are associated with the class, not any particular
instance. As a result they are "shared" between all instances of the class
(if there are any); or looked at another way, you address the values through
the class rather than an instance of the class. Hence, the values are the
same (shared) no matter who is looking at them.

Consider a class, Foo, with a static member, StaticInt, and an instance
(non-static) member, InstanceInt. Let's say they are both initially zero:

Foo myFoo1 = new Foo();
Foo myFoo2 = new Foo();
Console.WriteLine(myFoo1.InstanceInt); // zero
Console.WriteLine(myFoo2.InstanceInt); // zero
Console.WriteLine(Foo.StaticInt); // also zero; note we address it via the
class, not the instance
Foo.StaticInt = 2;
myFoo1.InstanceInt++;
Console.WriteLine(myFoo1.InstanceInt); // 1
Console.WriteLine(myFoo2.InstanceInt); // zero
Console.WriteLine(Foo.StaticInt); // 2

Note that InstanceInt has an independent value for each instance, but there
is but one StaticInt.

--Bob

"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:ji********************************@4ax.com...
Wait, I may have answered my own question. I have been thinking of
'static' much the way I do on 'const'. I was thinking that the value
held by a 'static' variable could not be changed. But, if I'm
thinking correctly now, I believe that the limiktation of a 'static'
value is that it can not be destroyed and re-created (I'm sure I'm
using the wrong terms here). So, I can initialize the static font
object using the constructor, as I am prone to do. I can't do that
again on that static object, but I can change the font size property
of that object.

Well, I'll go run a test and I'll know. I just wanted to state that I
believe I have been looking at this problem all wrong.

Thanks,
-Neil K.
On Sat, 02 Oct 2004 17:50:32 -0500, Neil Kiser
<neil@NO_SPAM_PLEASEkiserville.com> wrote:
Thank you very much. This helps to straighten me out.

One problem I see is that if I create the class with static members, I
have now realized that if the user changes their font selection that I
will need to have them exit and re-start the application in order for
the changes to take effect.

Is there no other mechanism I can use? My fundamental goal is to
create something that is globally available to all objects without
each of them having to create their own instance of it. But I want to
be able to change the values held by this object/class during runtime.

I have this sense that I'm sitting on a brain fart and that the
solution is known to me. Any assistance is greatly appreciated.

-Neil K.

Nov 16 '05 #7
Hi Neil:

You are right, do not think of static as meaning 'const'. Static means
the member belongs the the type, rather than an instance of the type.
You can still read and write to a static field or property.

--
Scott
http://www.OdeToCode.com/

On Sat, 02 Oct 2004 18:01:24 -0500, Neil Kiser
<neil@NO_SPAM_PLEASEkiserville.com> wrote:
Wait, I may have answered my own question. I have been thinking of
'static' much the way I do on 'const'. I was thinking that the value
held by a 'static' variable could not be changed. But, if I'm
thinking correctly now, I believe that the limiktation of a 'static'
value is that it can not be destroyed and re-created (I'm sure I'm
using the wrong terms here). So, I can initialize the static font
object using the constructor, as I am prone to do. I can't do that
again on that static object, but I can change the font size property
of that object.

Well, I'll go run a test and I'll know. I just wanted to state that I
believe I have been looking at this problem all wrong.

Thanks,
-Neil K.


Nov 16 '05 #8
You may want to look into the "singleton design pattern" too (do a search) -
it's a way to make a class so that you can only create exactly one instance
of it. It's almost the same thing, but then the class will look and act (and
need to be instantiated) like a regular class, but any "new" instances would
just point to the existing class..

"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:nl********************************@4ax.com...
Thank you very much. This helps to straighten me out.

One problem I see is that if I create the class with static members, I
have now realized that if the user changes their font selection that I
will need to have them exit and re-start the application in order for
the changes to take effect.

Is there no other mechanism I can use? My fundamental goal is to
create something that is globally available to all objects without
each of them having to create their own instance of it. But I want to
be able to change the values held by this object/class during runtime.

I have this sense that I'm sitting on a brain fart and that the
solution is known to me. Any assistance is greatly appreciated.

-Neil K.
On Sat, 2 Oct 2004 07:25:57 -0700, "Bob Grommes" <bo*@bobgrommes.com>
wrote:
There is no such thing as a static class, although class can have a static
constructor and/or static members -- and it can have those and nothing
else
if you choose.

Static members are available without having to create in instance of the
class.

If you create a class with a static constructor that fetches your registry
info and stores the info in static members, then yes, all your forms can
access this info by referencing the name of the class but not
instantiating
it:

--Bob
"Neil Kiser" <neil@NO_SPAM_PLEASEkiserville.com> wrote in message
news:8v********************************@4ax.com. ..
I'm trying to understand what defining a class as 'static' does for
me.

Here's an example, because maybe I am thinking about this all wrong:

My app will allows the user to control the fonts that the app uses.
So I will need to change the fonts depending on what settings the user
has entered. However, it seems kind of wasteful to me to go to teh
registry, fetch the font information and create new font objects for
every form that I am going to display. So I was thinking that perhaps
the 'static' keyword held my salvation somehow. I'd like to have a
clas that reads from the registry, creates the fonts, and then they
are there for any of the forms to reference.

Any comments on that would be greatly appreciated.

Thanks,
-Neil K.


Nov 16 '05 #9
In C# 2.0, you can have a static class, which means that the class will
not have any other member but static members, which is not the case in
C# 1.1. So,look into C# 2.0 if you are stuck about "static" classes.
FYI, the C# 2.0 static classes are like "public abstract sealed static
class MyClass" (Non-instantiable, non-inheritable and static).

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

6
by: Jahn Otto Næsgaard Andersen | last post by:
Hi, I have a class A with a static pointer to some data. The pointer is static because I want to access the data from all instances of the class. The data itself is allocated when the first...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
4
by: Pat | last post by:
I would like to know what is the meaning of : static vector<int> v; What is the difference with: vector<int> v;
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
3
by: ruud.bos | last post by:
Hi list, As a C++ newbie, I have a question about static member functions. Suppose I have the following class definition: class MyClass { public: static void MyFunc(); };
9
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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,...

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.