Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 305785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:24:22+00:00 2026-05-12T07:24:22+00:00

OK, so I have a database field of type char(1) that has a small

  • 0

OK, so I have a database field of type char(1) that has a small number of possible state codes (e.g. ‘F’= Failure, ‘U’=Unknown, etc.). I’d like to have a C# enum class that corresponds to these states. I can do:

public enum StatusCode : byte {
    Unknown = (byte) 'U',
    Failure = (byte) 'F',
    // etc.
}

So far so good. But in the DataTable returned from the database, the column values are System.Data.SqlTypes.SqlString instances. There are obviously some issues converting from a C# string (or even a C# char) to a C# byte (since C# char is really a UTF-16 codepoint). But in this case I know the values are constrained to a small set, and the code should throw an exception if a value outside this set comes through.

With that in mind, what’s the best way of doing this? Is it safe to cast from a SqlString to a byte? Would Convert.ToByte() be better? Would it be better to simply use a switch/case construct to crosswalk the values into the enum?

I’m looking for the “best” way to do this, not only in terms of getting the right results but also for code clarity. I suppose I could also just use some constants like

public const char UnknownStatus = 'U';
public const char FailureStatus = 'F';

But I’d rather use an enum if possible. Any thoughts?

Edit: To clarify what I want do do with this, I’m expecting to use these values frequently throughout my code. For example, I want to be able to do things like:

public void DoSomething(StatusCode currentStatus) {
    if(currentStatus == StatusCode.Failure) {
        throw new SomeException();
    }

    switch(currentStatus) {
        case StatusCode.Unknown:
            // do something
            break;
    }
}

And so forth. I particularly want to avoid things like:

public void DoSomething(char currentStatus) {
    if(currentStatus == 'F') {
        // do something
    }
}

Since in this case I’m using what amounts to “magic numbers” all over the place. In particular, this would make migrating to some other state-flagging system virtually impossible. Does that make sense?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:24:22+00:00Added an answer on May 12, 2026 at 7:24 am

    Maybe a “constant” object?

    public sealed class StatusCode {
        private char value;
    
        public static readonly StatusCode Unknown = new StatusCode('U');
        public static readonly StatusCode Failure = new StatusCode('F');
    
        private StatusCode(char v) {
            value = v;
        }
    
        public override string ToString() {
            return value.ToString();
        }
    
    }
    

    Then, later in your code, you could use it like an enum: StatusCode.Unknown. You could also provide an internal method to ‘parse’ a received value into an object of StatusCode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a field of type date within a MySQL database.
I have little doubt, I have a field of type oid in my database
I have a database table with a field that I need to read from
I have a database which has a NOT NULL constraint on a field, and
I have a database field of type time with time zone . How can
I have a numeric database field (numeric(3) in SQL Server 2000) that allows nulls,
I have a database table with date field whose data type is also date
Hi all i have my database structure as follows Field Type FileHeader longblob BatchHeader
I have following database schema for tabel tblpers Field Type Null Key Default Extra
I have a mysql database, with two fields (firstname, lastname) of type CHAR(255) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.