SamuelWest
SamuelWest

Reputation: 79

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?

Upvotes: 2

Views: 100

Answers (4)

Tim Southard
Tim Southard

Reputation: 614

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

Upvotes: 4

HelloWorld
HelloWorld

Reputation: 1103

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
}

Upvotes: 1

KHL
KHL

Reputation: 475

If a program uses set of inetgral numbers try to use C# enums.

https://msdn.microsoft.com/fr-fr/library/sbbt4032.aspx http://www.tutorialspoint.com/csharp/csharp_enums.htm

Upvotes: 0

Mike Cole
Mike Cole

Reputation: 14723

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

Upvotes: 1

Related Questions