ascii - How to implement C# enum for enumerated char(1) database field? -


Ok, so I have a database field that has a small number of potential state codes in type four (1) (for example 'F' = failure, 'U' = unknown, etc.) I have a C # enum class Which corresponds to these states. I can:

  public enum statuscode: byte {unknown = (byte) 'u', failure = (byte) 'f', // etc}  

So far so good but returned from the database in the datatable, the column values ​​are System.Data.SqlTypes.SqlString example is obviously some C # string (or a C # variable) from C # byte (actually from C # variable There are some issues for converting for a UTF-16 codepoint) but in this case I know that the value is constrained for a small set. And the code should be thrown out of the exception if the value outside this set comes.

With it, what's the best way to do this? Is it safe to put on a byte with SqlString? Will convert Would it be better to use just a switch / case to crosswalk them to the values?

I'm not only looking for the "best" way of doing this in terms of getting the right result but also for code clarity. I think I can also use some constants such as

  public const char UnknownStatus = 'U'; Public const char failure = 'f';  

But if possible, I want to use an enum any ideas?

EDIT: To clarify what I want to do with it, I expect these codes to be used repeatedly in my code. For example, I have to work this way I want to be able to:

  Public Zero DoSomething (StatusCode currentStatus) {if (currentStatus == StatusCode.Failure) {New some expansion (); } Switch (current status) {case StatusCode.Unknown: // break something; }}  

And so forth I want to avoid things like:

  public zero doSomething (four current state) {if (current status == 'F') {// do something}}  

From this situation I am using "magic numbers" equal to all the places. In particular, does it make sense to get rid of any other state-flagging system?

Maybe a "static" object?

  Public Sealed StatusCode {Private variable value; Statsod permission for public static reading = new statuscadet ('U'); Stats failure only for public static reading = new status ('F'); Private status (four V) {value = v; } Public override string ToString () {return value.ToString (); }}  

Then, in your code later, you can use it as an enum: StatusCode.Unknown . You can also provide an internal method for the 'pars' of the received value in the StatusCode object.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -