开发者

How to use combination of enums in switch case in c#?

开发者 https://www.devze.com 2023-01-05 01:39 出处:网络
I have following enum definition public enum UploaderType { BrandLogo = 0, ReportingLogo = 1, DocumentTemplate = 2,

I have following enum definition

public enum UploaderType
{
    BrandLogo = 0,
    ReportingLogo = 1,
    DocumentTemplate = 2,
    MModalTemplate = 3,
}

I have a switch case in which i want to use this enum

void FileUploadExceptionHandler(FileUploadControl.FileUploadExceptionType     exceptionType, FileUploadControl.UploaderType uploaderType)
{
    switch (uploaderType)
    {
        case FileUploadControl.UploaderType.DocumentTemplate:
            (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionError").ToString(), docIndentifier, formatAllowed(ucdocxUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
            break;
        case FileUploadControl.UploaderType.MModalTemplate:
            (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionError").ToString(), docIndentifier, formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
            break;
        case FileUploadControl.UploaderType.MModalTemplate|FileUploadControl.UploaderType.DocumentTemplate:
            (this.Page as PageBa开发者_运维技巧se).SetMessage(string.Format(GetLocalResourceObject("FileExtensionErrorForBoth").ToString(), formatAllowed(ucdocxUploadControl.SupportedFileTypes), formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
            break;
    }
}

I am trying to use combination of enum in third case but compiler complains The label 'case 3:' already occurs in this switch statement.

This enum is outside my control,so icannot use flags on it. Please suggest how to achieve this.

When i call it, i use

 FileUploadExceptionHandler(FileUploadControl.FileUploadExceptionType.FileExtensionNotAllowed, FileUploadControl.UploaderType.DocumentTemplate | FileUploadControl.UploaderType.MModalTemplate);


You can use fall through:

switch(enum)
{
    case MyEnum.First:
    case MyEnum.Second:
        // something
        break;
    case MyEnum.SomeThingElse:
        break;
}


Nobody suggested this yet, so it might be bad, or I may not understand the question, but I thought you can use goto for this:

switch (uploaderType)
{
    case FileUploadControl.UploaderType.DocumentTemplate:
        (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionError").ToString(), docIndentifier, formatAllowed(ucdocxUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
        break;
    case FileUploadControl.UploaderType.MModalTemplate:
        (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionError").ToString(), docIndentifier, formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
        goto case FileUploadControl.UploaderType.MModalTemplate;
    case FileUploadControl.UploaderType.DocumentTemplate:
        (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionErrorForBoth").ToString(), formatAllowed(ucdocxUploadControl.SupportedFileTypes), formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
        break;
}


You cannot do what you want. If you were able to modify the enum you would change it to this:

[Flags]
public enum UploaderType
{
    None = 0,
    BrandLogo = 1,
    ReportingLogo = 2,
    DocumentTemplate = 4,
    MModalTemplate = 8,
}

But since you can't change it, there's not much you can do.


mapping to a new enum with flags might help.

[Flags]
public enum UploaderTypeNew
{
    None = 0,
    BrandLogo = 1,
    ReportingLogo = 2,
    DocumentTemplate = 4,
    MModalTemplate = 8,
}

map each value of the old enum to the new enum and switch on the new enum.


You can have multiple statements per case. You can do this:

case FileUploadControl.UploaderType.DocumentTemplate:
    (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionError").ToString(), docIndentifier, formatAllowed(ucdocxUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
    break;
case FileUploadControl.UploaderType.MModalTemplate:
    (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionError").ToString(), docIndentifier, formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
    (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionErrorForBoth").ToString(), formatAllowed(ucdocxUploadControl.SupportedFileTypes), formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
    break;
case FileUploadControl.UploaderType.DocumentTemplate:
    (this.Page as PageBase).SetMessage(string.Format(GetLocalResourceObject("FileExtensionErrorForBoth").ToString(), formatAllowed(ucdocxUploadControl.SupportedFileTypes), formatAllowed(ucampUploadControl.SupportedFileTypes)), PageMessageType.ErrorMessage);
    break;


Given that you can't use flags,

(NModalTemplate | DocumentTemplate) == (2 | 3) == (3) == (NModalTemplate)

The last case in your switch doesn't make sense because you cannot possibly set the uploaderType to both values at the same time.

0

精彩评论

暂无评论...
验证码 换一张
取 消