简体   繁体   中英

Displaying a String rather than Int

Currently my code will display results from my database into labels. All of the results I get back are ints. I'd like to change that and make it, for example, 1 would be displayed as "Online".

I know I could write a bunch of "if" statements but that doesn't seem proper. Is there a better solution to this?

enum Colors { Red, Green, Blue, Yellow };

Then use

Enum.GetName(typeof(Colors), 3)

Keeping in mind that enums are zero based, unless a value is specified. MSDN

I use Headspring Enumeration for this. No reflection, extensible, flexible.

Use switch case , it's much better than a lot of if statements in this case.

switch(num)
{
    case 1:
        string = "Online";
    case 2:
        string = "Offline";
    default:
        string = "Default"; // Called if none of the cases above are true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM