static consts and inheritance

C

Chukkalove

Hi

I have an abstract class that contains 100% static methods and variables.

One of the member variables "string DatabaseName" needs to be overridden in
derived classes.
Am I able to keep my class as totally static and do this? If so then how
please?

(I dont want to create an instance of the derived class just to query a
const string)

thanks
Claire
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Chukkalove said:
Hi

I have an abstract class that contains 100% static methods and variables.

Why you have something like this? static could be seeing as the opposite of
abstract.
One of the member variables "string DatabaseName" needs to be overridden
in derived classes.
Am I able to keep my class as totally static and do this? If so then how
please?

Why you want to have them static in the first place?


Have you seeing the Singleton pattern? Maybe that's what you are after
 
B

Bruce Wood

Chukkalove said:
Hi

I have an abstract class that contains 100% static methods and variables.

In order that a class be abstract, it must have at least one abstract
member, and static members cannot be abstract, so I don't know how you
did this.
One of the member variables "string DatabaseName" needs to be overridden in
derived classes.

That makes no sense. "Override in derived classes" means polymorphism,
and polymorphism works only with class instances. Since your class is
entirely static, and therefore (presumably) you would want the
"overridden" thing to be static in the child class as well, I don't
understand what you're after.
Am I able to keep my class as totally static and do this?

Absolutely not. However, I think that the problem here is one of
terminology or design. Could you step back a few paces and describe the
effect you're trying to achieve and the problem you're trying to solve?
Maybe there's a different solution for your problem, or maybe we just
misunderstand what it is you need to do.
 
J

Jon Skeet [C# MVP]

Bruce Wood said:
In order that a class be abstract, it must have at least one abstract
member, and static members cannot be abstract, so I don't know how you
did this.

You don't have to have any abstract members to make a class abstract:

abstract class Test
{
static void Main() {}
}

compiles fine.

What doesn't make sense (amongst other things) is the idea of an
abstract *variable*. Variables can't be overridden...
 
J

Jon Shemitz

Chukkalove said:
I have an abstract class that contains 100% static methods and variables.

One of the member variables "string DatabaseName" needs to be overridden in
derived classes.
Am I able to keep my class as totally static and do this? If so then how
please?

abstract class StaticBase
{
public static string Name = "StaticBase";
}

abstract class StaticDerived : StaticBase
{
new public static string Name = "StaticDerived";
}


Note to other repliers: a "static" class would make more sense, in 2.0
.... except that a static class can only derive directly from object.
 

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