CodeDOM, const strings and abstract base classes

C

Chuck Bowling

I am using CodeDOM to generate source files. The classes being generated
have a const string member. This member is referenced in an abstract base
class but declared in the inheriting class. I can't declare the string in
the abstract class because it would require initialization. Is there a way
to require that any class inheriting from the base class contain this string
without a specific declaration in the base class? I tried putting a
declaration in an interface but the IDE still wants to see it initialized.
 
N

Nicholas Paldino [.NET/C# MVP]

Chuck,

Is this constant going to be different based on the derived type? If
so, then you you will have to declare each constant in each class.

However, I am unsure why you think that the abstract class requires
initialization? If it is a const, then that value will be initialized the
first time the type is referenced, and you don't need an instance of the
class for it to be referenced (a reference to the const itself on the type
will cause it to be initialized).

Hope this helps.
 
C

Chuck Bowling

Yes, the constant will be different for each derived class. The structure of
the classes is basically identical and the difference in behavior of each
class will depend mainly on the contents of this string. The reason I'd like
to declare the const in the abstract class is because I want to be able to
extend functionality in all the inheriting classes via the base class. The
methods in the base class will parse the contents of the const string and
change the behavior of the class accordingly.

The reason I'm led to believe that a const requires initialization in the
abstract base class is because I get an error when I try to compile with an
uninitialized const.

I want to reference an uninitialized const in the base class (or interface)
but define the const in the inheriting classes.


Nicholas Paldino said:
Chuck,

Is this constant going to be different based on the derived type? If
so, then you you will have to declare each constant in each class.

However, I am unsure why you think that the abstract class requires
initialization? If it is a const, then that value will be initialized the
first time the type is referenced, and you don't need an instance of the
class for it to be referenced (a reference to the const itself on the type
will cause it to be initialized).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck Bowling said:
I am using CodeDOM to generate source files. The classes being generated
have a const string member. This member is referenced in an abstract base
class but declared in the inheriting class. I can't declare the string in
the abstract class because it would require initialization. Is there a
way
to require that any class inheriting from the base class contain this
string
without a specific declaration in the base class? I tried putting a
declaration in an interface but the IDE still wants to see it
initialized.
 
C

Chris Jobson

I may be misunderstanding what you want to do, but could you make the string
an abstract property? Something like:

public abstract class MyBase {
protected abstract string MagicString {
get;
}

private void ParseMagicString() {
string s = MagicString;
// do appropriate things by parsing s
}
}

public class MyDerived1 : MyBase {
protected override string MagicString {
get {
return "abcdef";
}
}
}

public class MyDerived2 : MyBase {
protected override string MagicString {
get {
return "ghijkl";
}
}
}
etc.

Chris Jobson

Chuck Bowling said:
Yes, the constant will be different for each derived class. The structure
of the classes is basically identical and the difference in behavior of
each class will depend mainly on the contents of this string. The reason
I'd like to declare the const in the abstract class is because I want to
be able to extend functionality in all the inheriting classes via the base
class. The methods in the base class will parse the contents of the const
string and change the behavior of the class accordingly.

The reason I'm led to believe that a const requires initialization in the
abstract base class is because I get an error when I try to compile with
an uninitialized const.

I want to reference an uninitialized const in the base class (or
interface) but define the const in the inheriting classes.


Nicholas Paldino said:
Chuck,

Is this constant going to be different based on the derived type? If
so, then you you will have to declare each constant in each class.

However, I am unsure why you think that the abstract class requires
initialization? If it is a const, then that value will be initialized
the first time the type is referenced, and you don't need an instance of
the class for it to be referenced (a reference to the const itself on the
type will cause it to be initialized).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chuck Bowling said:
I am using CodeDOM to generate source files. The classes being generated
have a const string member. This member is referenced in an abstract
base
class but declared in the inheriting class. I can't declare the string
in
the abstract class because it would require initialization. Is there a
way
to require that any class inheriting from the base class contain this
string
without a specific declaration in the base class? I tried putting a
declaration in an interface but the IDE still wants to see it
initialized.
 
C

Chuck Bowling

Thanks for your help Chris. I decided to stop doing things the hard way and
went with a readonly string instead of a const. ;)

public abstract class Instrument {

public readonly string InstrumentString;

protected ArrayList noteList = new ArrayList();

public Instrument(string instrumentString) {

InstrumentString = instrumentString;

}

}

Chris Jobson said:
I may be misunderstanding what you want to do, but could you make the
string an abstract property? Something like:

public abstract class MyBase {
protected abstract string MagicString {
get;
}

private void ParseMagicString() {
string s = MagicString;
// do appropriate things by parsing s
}
}

public class MyDerived1 : MyBase {
protected override string MagicString {
get {
return "abcdef";
}
}
}

public class MyDerived2 : MyBase {
protected override string MagicString {
get {
return "ghijkl";
}
}
}
etc.

Chris Jobson

Chuck Bowling said:
Yes, the constant will be different for each derived class. The structure
of the classes is basically identical and the difference in behavior of
each class will depend mainly on the contents of this string. The reason
I'd like to declare the const in the abstract class is because I want to
be able to extend functionality in all the inheriting classes via the
base class. The methods in the base class will parse the contents of the
const string and change the behavior of the class accordingly.

The reason I'm led to believe that a const requires initialization in the
abstract base class is because I get an error when I try to compile with
an uninitialized const.

I want to reference an uninitialized const in the base class (or
interface) but define the const in the inheriting classes.


Nicholas Paldino said:
Chuck,

Is this constant going to be different based on the derived type? If
so, then you you will have to declare each constant in each class.

However, I am unsure why you think that the abstract class requires
initialization? If it is a const, then that value will be initialized
the first time the type is referenced, and you don't need an instance of
the class for it to be referenced (a reference to the const itself on
the type will cause it to be initialized).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am using CodeDOM to generate source files. The classes being generated
have a const string member. This member is referenced in an abstract
base
class but declared in the inheriting class. I can't declare the string
in
the abstract class because it would require initialization. Is there a
way
to require that any class inheriting from the base class contain this
string
without a specific declaration in the base class? I tried putting a
declaration in an interface but the IDE still wants to see it
initialized.
 

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