Question about "static class"

  • Thread starter Thread starter Neil Kiser
  • Start date Start date
N

Neil Kiser

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.
 
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
 
Bob Grommes said:
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, ;).
 
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/<#[email protected]>

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 said:
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]
 
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.
 
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.
 
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
 
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.
 
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..
 
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
 
Back
Top