Enums Type Question

J

Just Me

I keep hitting the same minor problem, but dont have a really great
solution. So Im looking for ideas.



There are many instances where I want to force the developer ( me ) from a
readability and robustability point of view to choose from a list of
predefined choices when calling functions. Well, of course enums are good
for this, so I could have something like this. In which a webfrom textboxm
gets a css class attribute which equals the name of the enum member.

public enum messageType {fail=0,pass=1,warning=3}


public void message( string msgStr, messageType msgType )
{


myTextBox.Attributes.add("class",msgType.toString());
myTextBox.Text=msgStr;


}

// However. I would like to abstract the name used by the developer and the
actuall css class applied. Any suggestions on how to do this.

I thought instead of using an enum, I could use constant members but how
could I force the developer to enter the classtype with the constants in ?

Im sure someone has a groovy easy answer ! :)
 
I

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

Hi,

I think that the enums are the best way to do what you want.

I do not understand what you mean with abstract the name.

You cuold create CSS that have a similar naming than the enum, for example
if the enum is Fail, the css could be FailCSS or something similar
 
N

Nicholas Paldino [.NET/C# MVP]

Are you looking to abstract it for display purposes, or because you want
a different list in Intellisense? If you want it for display purposes, you
could always create a list/map of other class instances which expose the
abstracted value (from any other data source you wish) as well as the enum
value. Then it is just a matter of a simple lookup.
 
J

Just Me

The purpose was to abstract the name the programmer would see when choosing
one of several enumerations, but the actual value would be different and
would represent the real css class to be applied to the control when the
page is rendered.

Its really a nice to have.


Nicholas Paldino said:
Are you looking to abstract it for display purposes, or because you
want a different list in Intellisense? If you want it for display
purposes, you could always create a list/map of other class instances
which expose the abstracted value (from any other data source you wish) as
well as the enum value. Then it is just a matter of a simple lookup.


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

Just Me said:
I keep hitting the same minor problem, but dont have a really great
solution. So Im looking for ideas.



There are many instances where I want to force the developer ( me ) from
a readability and robustability point of view to choose from a list of
predefined choices when calling functions. Well, of course enums are good
for this, so I could have something like this. In which a webfrom
textboxm gets a css class attribute which equals the name of the enum
member.

public enum messageType {fail=0,pass=1,warning=3}


public void message( string msgStr, messageType msgType )
{


myTextBox.Attributes.add("class",msgType.toString());
myTextBox.Text=msgStr;


}

// However. I would like to abstract the name used by the developer and
the actuall css class applied. Any suggestions on how to do this.

I thought instead of using an enum, I could use constant members but how
could I force the developer to enter the classtype with the constants in
?

Im sure someone has a groovy easy answer ! :)
 

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