static virtual

M

Mark

I know that you can't have a static virtual property, but is there a
way to simulate the same results?

I have a base class that I want to extend so that you can change a
value and it inherits all of the methods to work with that value.
That's easy enough:

class BaseC {
string val;

public DoSomething() {
//manipulate val
}
}

class Derived : BaseC {
public Derived() {
val = "foo"
}
}

Derived.DoSomething() now manipulates "foo"

Now if I want BaseC to have static methods, that also manipulate
'val', I have to make val static. But when I have multiple derived
classes with static constructors, the static variable is for all of
the derived classes (bad).

I thought I could make static virtual properties that could just be
overloaded in the derived classes to return the correct value, but as
I stated, static virtual is not allowed.

Any Ideas?
Mark
 
G

Guest

Try using the singleton design pattern. Make the Base a singleton. When
derived types get constructed, a single base is created for that derv
instance exclusively. This derv instance could manipulate the variable
originally defined in Base, and would not meddle with other derv's definition
as well.
 
R

RayO

I don't see how static virtual makes any sense.
How do you expect to manipulate instance variables of
a class from static methods of the same class?!?!


RayO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top