H
Hans Kesting
Hi,
In a database some code is stored as a string. This code can have a
limited (known) set of values. What is the best way to use this in my
(program-)code?
I don't want to use "magic codes" like
CustomerInterface.StoreLevel(CustomerID, "BEH");
because this would give no indication what sort of string was expected
and what the accepted values were.
I could declare a const:
public const string Level1 = "BEH";
and use it like
CustomerInterface.StoreLevel(CustomerID, CustomerInterface.Level1);
This way the magic code is somewhat under control, but still there
would be no way to prevent someone from using an unknown string like
"XYZ". The compiler would not complain, but a runtime error would
(probably) follow. There would be no way to guarantee that one of the
"CustomerInterface" conts is used.
Third option, use an enum:
public enum Levels { Low, Medium, ... };
CustomerInterface.StoreLevel(CustomerID, Levels.Level1);
This way the compiler would make sure I really use a "Levels" value
(OK, I could use '(Levels)6' but that would be cheating).
The downside is that somewhere I would have to translate from this
Levels enum to the string code because that database only accepts a
string, and that can't be changed. So I use a separate class:
class EnumConverter
{
public static string LevelToCode(Levels theLevel)
{
switch (theLevel)
{
case Levels.Low: return "BEH";
case Levels.Medium: return "EO";
...
}
}
}
But this seems to me more like Oh-Oh programming than the OO version.
Does anyone know a better way where I can specify the "level" in a
typesafe way and encapsulate the "ToCode" and "ParseFromCode" methods?
Thanks,
Hans Kesting
In a database some code is stored as a string. This code can have a
limited (known) set of values. What is the best way to use this in my
(program-)code?
I don't want to use "magic codes" like
CustomerInterface.StoreLevel(CustomerID, "BEH");
because this would give no indication what sort of string was expected
and what the accepted values were.
I could declare a const:
public const string Level1 = "BEH";
and use it like
CustomerInterface.StoreLevel(CustomerID, CustomerInterface.Level1);
This way the magic code is somewhat under control, but still there
would be no way to prevent someone from using an unknown string like
"XYZ". The compiler would not complain, but a runtime error would
(probably) follow. There would be no way to guarantee that one of the
"CustomerInterface" conts is used.
Third option, use an enum:
public enum Levels { Low, Medium, ... };
CustomerInterface.StoreLevel(CustomerID, Levels.Level1);
This way the compiler would make sure I really use a "Levels" value
(OK, I could use '(Levels)6' but that would be cheating).
The downside is that somewhere I would have to translate from this
Levels enum to the string code because that database only accepts a
string, and that can't be changed. So I use a separate class:
class EnumConverter
{
public static string LevelToCode(Levels theLevel)
{
switch (theLevel)
{
case Levels.Low: return "BEH";
case Levels.Medium: return "EO";
...
}
}
}
But this seems to me more like Oh-Oh programming than the OO version.
Does anyone know a better way where I can specify the "level" in a
typesafe way and encapsulate the "ToCode" and "ParseFromCode" methods?
Thanks,
Hans Kesting