views:

308

answers:

1

I have the following enum -->

    public enum SyncStatus
    {
        Unavailable = 0,
        Checking = 5,
        StartedAspNetDb = 10,
        FinishedAspNetDb = 20,
        StartedMatrixDb = 30,
        FinishedMatrixDb = 40,
        StartedConnectDb = 50,
        FinishedConnectDb = 60,
        StartedCmoDb = 70,
        FinishedCmoDb = 80,
        StartedMcpDb = 90,
        FinishedMcpDb = 100
    }

Which I use here -->

        SyncInBackground.ReportProgress(SyncStatus.StartedAspNetDb);
        MergeRepl aspnetdbMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "aspnetdb", "aspnetdb", "aspnetdb");
        aspnetdbMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedAspNetDb);

        SyncInBackground.ReportProgress(SyncStatus.StartedMatrixDb);
        MergeRepl matrixMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "MATRIX", "MATRIX", "MATRIX");
        matrixMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedMatrixDb);

        SyncInBackground.ReportProgress(SyncStatus.StartedConnectDb);
        MergeRepl connectMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "CONNECT", "Connect", "Connect");
        connectMergeRepl.RunDataSync();
        SyncInBackground.ReportProgress(SyncStatus.FinishedConnectDb);

What I don't understand is why, if int is default enum governing type, do I have to CAST this line, like so -->

 SyncInBackground.ReportProgress((int)SyncStatus.Checking);

Forgive my ignorance I just like to understand the why of things not just that they are so.

+9  A: 

There just isn't an implicit conversion from an enum type to its underlying type. This makes it harder to accidentally use an enum as its numeric value.

(There's likewise no conversion the other way.)

There is an implicit conversion from the constant 0 to any enum type, by the way.

Oh, and unboxing from a boxed enum value to its underlying type - or vice versa - works too. At least at one point this was really badly mangled in both the CLI spec and the C# spec; it may well have been fixed by now :)

EDIT:

Here's an alternative option if you actually just want to use the values as numbers:

public static class SyncStatus
{
    public const int Unavailable = 0;
    public const int Checking = 5;
    public const int StartedAspNetDb = 10;
    public const int FinishedAspNetDb = 20;
    public const int StartedMatrixDb = 30;
    public const int FinishedMatrixDb = 40;
    public const int StartedConnectDb = 50;
    public const int FinishedConnectDb = 60;
    public const int StartedCmoDb = 70;
    public const int FinishedCmoDb = 80;
    public const int StartedMcpDb = 90;
    public const int FinishedMcpDb = 100;
}

Alternatively, write a method like this:

static void ReportProgress(SyncStatus status)
{
    SyncInBackground.ReportProgress((int) status);
}
Jon Skeet
So, no matter what Type I might make my enum I will always have to Cast it to that type where I might need to use it? The reasoning is to protect me from myself? Am I correct?
Refracted Paladin
Yup. If you want integers, use a static class with constants instead.
Jon Skeet
:) Thanks, my ultimate goal was to be able to use the `int` for a progressbar and the `string` portion for a label control. Seems like I may have been using the wrong tool....
Refracted Paladin
@Pal, you can do what you want just use the last example above. Pass your enum type and when you need the integer value cast it to int and when you want the string representation explicitly call ToString() (or rely on an implicit conversion).
Tuzo
@Tuzo: When I do a ToString on an enum I get the Text Description, not the number as a string... I didn't know that. Thanks!
Refracted Paladin