473,545 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Statics and inheritance

Let's say I have a root class called RootBusinessSer vice
and I then want to have 25 business service classes based off of it.

And each class has a property that's shared between all instances of it
- called 'State'. I therefore want all of them to have a static
property State.

However, I can't declare this static property in RootBusinessSer vice -
because if I do, the subclasses will all point to that single property.

So I have to declare it in each individual class. 25 times.

Not only that, but if I want it to come up in intellisense for all
subclasses of RootBusinessSer ver, I need to create a get/set in the
root, and override it in each subclass to point to the 'local' static
property.

Is this right, or am I missing a really easy way to have a static
property automatically exist in all subclasses of a given class?

Cheers,

Andy D

Nov 17 '05 #1
6 1841

As long as you're accessing the data through an instance of a base
class and not through the base class itself, then one option is to use
a static dictionary in the base. Something along these lines..

class Base {
private static IDictionary _stuff = new Hashtable();
public object Stuff {
get {
return _stuff[this.GetType()];
}
set {
_stuff[this.GetType()] = value;
}
}

class A : Base {}
class B: Base {}

With this architecture, the base will declare a single property which
will get and set a value that is shared across instances of a
particular type.

HTH,

Sam

On 31 May 2005 05:29:55 -0700, "Andrew Ducker" <an****@ducker. org.uk>
wrote:
Let's say I have a root class called RootBusinessSer vice
and I then want to have 25 business service classes based off of it.

And each class has a property that's shared between all instances of it
- called 'State'. I therefore want all of them to have a static
property State.

However, I can't declare this static property in RootBusinessSer vice -
because if I do, the subclasses will all point to that single property.

So I have to declare it in each individual class. 25 times.

Not only that, but if I want it to come up in intellisense for all
subclasses of RootBusinessSer ver, I need to create a get/set in the
root, and override it in each subclass to point to the 'local' static
property.

Is this right, or am I missing a really easy way to have a static
property automatically exist in all subclasses of a given class?

Cheers,

Andy D


Nov 17 '05 #2
Andrew Ducker wrote:

[...snip...]
And each class has a property that's shared between all instances of it
- called 'State'. I therefore want all of them to have a static
property State.

However, I can't declare this static property in RootBusinessSer vice -
because if I do, the subclasses will all point to that single property.

So I have to declare it in each individual class. 25 times.

Not only that, but if I want it to come up in intellisense for all
subclasses of RootBusinessSer ver, I need to create a get/set in the
root, and override it in each subclass to point to the 'local' static
property.

[...snip...]

That's why some other languages implement things like "class instances
variables" - class (static) variables being inherited by all subclasses, but
not with a shared content. Each subclass has its own value, and access
methods aren't necessary outside the base class.

The inventors of c# (and java, btw) "borrowed" a lot of things from other
languages, but "forgot to borrow" some very nice features of them.
Nov 17 '05 #3
Hi,

Well, if you declare it in Root then you are implying that ALL the derived
classes share the very same value.
If you happen to have the same escenario on each of the derived classes,
then you would have to do the same thing on each one, remember each of them
happen independely of the others.

Not very complicated to do in any case.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andrew Ducker" <an****@ducker. org.uk> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Let's say I have a root class called RootBusinessSer vice
and I then want to have 25 business service classes based off of it.

And each class has a property that's shared between all instances of it
- called 'State'. I therefore want all of them to have a static
property State.

However, I can't declare this static property in RootBusinessSer vice -
because if I do, the subclasses will all point to that single property.

So I have to declare it in each individual class. 25 times.

Not only that, but if I want it to come up in intellisense for all
subclasses of RootBusinessSer ver, I need to create a get/set in the
root, and override it in each subclass to point to the 'local' static
property.

Is this right, or am I missing a really easy way to have a static
property automatically exist in all subclasses of a given class?

Cheers,

Andy D

Nov 17 '05 #4
You're a genius! I just ripped out code from 25 places and replaced it
with that code.
Well, actually I added in a null check, giving me this:

public BusinessCallSta te CurrentState
{
get
{
object possibleState = states[this.GetType()];
if (possibleState == null)
return BusinessCallSta te.Uncalled;
else
return (BusinessCallSt ate)possibleSta te;
}
set
{
states[this.GetType()] = value;
}
}

But generally speaking that was perfect!

Cheers,

Andy D

Nov 17 '05 #5
this raises an interesting design question.

With this scenario, every subclass of the base (direct or indirect) has
access to this dictionary object.
Have you achieved encapsulation?

From a pure design standpoint, haven't you coupled the base class to the
existence of each of its children (if not the active awareness of them)?

To be honest, I'm not sure. Please share your opinions.

The design suggested is effectively a non-thread-safe Singleton. (Note that
the Singleton pattern doesn't require that only one element exist. It
requires that the creation of the element be managed. The GoF book
specifically mentions the possibility of using the Singleton to manage a set
of objects, not just a single one.)

To reduce coupling in the design, I would suggest that you could have your
state managed by a StateManager object that implements this mechanism,
rather than by the base class itself. Also, see Jon Skeet's page on
Singletons.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Samuel R. Neff" <in**********@n ewsgroup.nospam > wrote in message
news:ju******** *************** *********@4ax.c om...

As long as you're accessing the data through an instance of a base
class and not through the base class itself, then one option is to use
a static dictionary in the base. Something along these lines..

class Base {
private static IDictionary _stuff = new Hashtable();
public object Stuff {
get {
return _stuff[this.GetType()];
}
set {
_stuff[this.GetType()] = value;
}
}

class A : Base {}
class B: Base {}

With this architecture, the base will declare a single property which
will get and set a value that is shared across instances of a
particular type.

HTH,

Sam

On 31 May 2005 05:29:55 -0700, "Andrew Ducker" <an****@ducker. org.uk>
wrote:
Let's say I have a root class called RootBusinessSer vice
and I then want to have 25 business service classes based off of it.

And each class has a property that's shared between all instances of it
- called 'State'. I therefore want all of them to have a static
property State.

However, I can't declare this static property in RootBusinessSer vice -
because if I do, the subclasses will all point to that single property.

So I have to declare it in each individual class. 25 times.

Not only that, but if I want it to come up in intellisense for all
subclasses of RootBusinessSer ver, I need to create a get/set in the
root, and override it in each subclass to point to the 'local' static
property.

Is this right, or am I missing a really easy way to have a static
property automatically exist in all subclasses of a given class?

Cheers,

Andy D

Nov 17 '05 #6
The dictionary itself is private to the base class, so the children
don't have access to it except for their specific value (as defined by
their type). In the example I just made it "object" but obviously it
could be anything. The base class can even enforce that it is a
specific state type.

The base class is not really coupled to the existence of child items
because the base class itself has it's own shared object based on its
own type. Any number of derived classes can be written and each will
have its own shared store.

The only real coupling issue I see is if a child class has its own
child class and they wish to share a static store--that is not
possible in this implementation. It is achievable, but is more
complicated and was not specifcied as a requirement in the original
question.

This is closer to a Flyweight than a Singleton. I'll have to go back
and re-read the GoF text on Singleton for managing multiple instances
because my understanding is that is the exact definition of a
Flyweight--also defined by GoF.

It's not thread safe but the original question was not related to
threading--it was how to achieve shared values across instances and
still have the member defined in the base class and inherited.
Thread-safety can be added and is not a core component of the question
or answer.

Thanks,

Sam
On Tue, 31 May 2005 09:57:45 -0700, "Nick Malik [Microsoft]"
<ni*******@hotm ail.nospam.com> wrote:
this raises an interesting design question.

With this scenario, every subclass of the base (direct or indirect) has
access to this dictionary object.
Have you achieved encapsulation?

From a pure design standpoint, haven't you coupled the base class to the
existence of each of its children (if not the active awareness of them)?

To be honest, I'm not sure. Please share your opinions.

The design suggested is effectively a non-thread-safe Singleton. (Note that
the Singleton pattern doesn't require that only one element exist. It
requires that the creation of the element be managed. The GoF book
specifically mentions the possibility of using the Singleton to manage a set
of objects, not just a single one.)

To reduce coupling in the design, I would suggest that you could have your
state managed by a StateManager object that implements this mechanism,
rather than by the base class itself. Also, see Jon Skeet's page on
Singletons.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik


Nov 17 '05 #7

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

Similar topics

6
1588
by: Spacen Jasset | last post by:
Since it says in the standard that the order of initialization of variables is indeterminate, does this also apply to a static within a function? e.g. int GetInt() { static int test = 458; return test;
4
1632
by: Martin | last post by:
I have some classes with statics public variables.... the problem is that the gcc compiles fine the files but in the linking process gcc says "multiple definitions of MyClass::Variable" or "undefined definition of MyClass::Variable". But this problem is only with static variables within a class. I access to variables with the complete name. I...
5
1772
by: Stuart MacMartin | last post by:
I have a problem with static lifetime (order of destruction of statics within different cpp files). I have a workaround that happens to work in my case. I'd like to know if this is luck or required to work. Consider: class A { ... static B m_b; static C& GetC(); };
2
1522
by: Andrew Ducker | last post by:
I'm implementing a singleton using the example at: http://www.yoda.arachsys.com/csharp/singleton.html as a basis (second example). However - I have about 20 classes I wish to make singletons - and I don't want to duplicate the code in each one. The difficulty comes in the method that returns the singleton instance. In the example it's:...
20
5981
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static members / singletons (especially meyer type singletons) which use locally declared static variables. These variables are normally cleaned up...
3
2142
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little static shared variable here and there without having to compromise safety in the Sql Server 2005.
3
1418
by: DR | last post by:
I heard there is some trick to referencing statics in VB.NET CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little static shared variable here and there without having to compromise safety in the Sql Server 2005.
3
2159
by: Peter | last post by:
Hi I can create types from dynamic loaaded assemblies (Assembly.Load) with Assembly.GetTyp( <typename) - OK I need a reference to the assembly (just Type.GetType() do not work ) now my question. 1) If i need different Types/Objects of the same Assembly in different app.sections, should I use Assembly.Load() several times or shoud I
1
1369
by: tropos | last post by:
I work on a large Apache plugin for an Internet Bank. Logging shows that many or all static objects in the plugin are being initialised twice at startup time. The platform is C++, AIX Unix, using the vaccp compiler. The plugin is built as a shared object (.so) file. My first thought was that Apache's parent process loads the shared...
3
1379
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... At least by the group title, this seems like a question for dotnet.framework.aspnet.caching but that group seems pretty slow. I'm trying to sort things out with a co-worker. We've got some classes that implement some in-memory caches with static Hashtable members. My understanding is that a static Hashtable may be shared between...
0
7468
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
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
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
7423
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
5972
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
5329
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
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
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 we have to send another system
0
704
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.