By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

CodeDOM, const strings and abstract base classes

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #2

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.


Nov 16 '05 #3

P: n/a
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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:Os**************@TK2MSFTNGP12.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #4

P: n/a
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" <ch**********@btinternet.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
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" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:Os**************@TK2MSFTNGP12.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Chuck Bowling" <ch**********@sbcglobal-NO-SPAM.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.