473,242 Members | 1,481 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,242 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 2269
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.