简体   繁体   中英

Where to use enum and how to simulate the messageboxICon enum

I want to know why and where should we use the enumerators,i have a set of images i want to assign numbers to each and put it in a enum and i want to set the number instead of entire filename when i want to set the image ,just like MessageBoxIcon enum

Thanks

I presume you mean enumerations rather than enumerators . Enums are used to specify a discrete collection of possible symbolic values or meanings. Are you sure this is what you want? Note that enums are baked in at compile time and can't be changed afterwards, so if your set of images is going to change at runtime, you'll need some other data structure.

Also note that enums can't contain any information other than the names of their members, so you wouldn't be able to associate file names with them, for example. To do so, you'd need a Dictionary or something similar, eg:

enum Image
{
    Image1,
    Image2,
    Image3
}

An in some method:

var imageFileNames = new Dictionary<Image, string>();
imageFileNames[Image.Image1] = "/path/to/file1.jpg";
imageFileNames[Image.Image2] = "/path/to/file2.jpg";
imageFileNames[Image.Image3] = "/path/to/file3.jpg";

Enum is an explicit listing of all possible values of a set . You can use enums whenever you have a set of values to choose from. Another popular use of enums is to set flags

For your problem, I believe enums may not be the best way to go, as I filenames may contain characters not allowed in c# code. Although You can use the Enum descriptions hack to do this. Just make the GetDescription method an extension method by adding this keyword to the argument.

I highly recommend that you store the images you want to be able to choose from in your C# project's Resources file. (See my answer here for how to do this.)

Then, you need to create an enumeration (which is just a listing that defines all of the possible values in a set) in your code. I recommend creating a new, empty file in your project to hold your enumeration and naming it the same as the enum so that you will be able to find it again easily. Your enumeration file will look something like this (you can add as many values as you want):

public enum PartyImage
{
    None = 0,
    SmileyFace = 1,
    Flower = 2,
    Balloon = 3
}

Then, wherever you want to specify the image to be displayed, you can reference one of the enum values. In the code that is going to handle displaying the image, you'll have to use a switch statement that will translate the enum value that you specified to an image file that is stored in your project resources:

private void FlowerButtonClick()
{
   //When this button is clicked, change the displayed image to a flower picture
   UpdateDisplayedImage(PartyImage.Flower);   
}

//etc.

public void UpdateDisplayedImage(PartyImage image)
{
    //Determine the image that was specified in the parameter,
    //and update my picture box accordingly
    switch (PartyImage)
    {
        case None:
            myPicBox.Image = null;
            break;
        case Smileyface:
            myPicBox.Image = MyWindowsApp.Properties.Resources.SmileyFace;
            break;
        case Flower:
            myPicBox.Image = MyWindowsApp.Properties.Resources.Flower;
            break;
        case Balloon:
            myPicBox.Image = MyWindowsApp.Properties.Resources.Balloon;
            break;
    }
}

In the example above, the FlowerButtonClick method simply specifies that you want the image called Flower in the PartyImage enum to be displayed. The code it contains is equivalent to calling the MessageBox.Show function and specifying the image you want to appear on the message box. You can have as many of these methods as you want, and they can be anywhere in your code, just like you can display a message box with a specified icon from anywhere.

The UpdateDisplayedImage method does the actual work of retrieving the image from your project's Resources file that corresponds to the enum value that you specified and displaying it. This is equivalent to the work that is done internally by the MessageBox.Show function when it creates and displays a new message box with the specified icon.


Recommended reading for more information about enumerations:

  1. Enumeration Design (MSDN)

  2. Enums in C# (MSDN)

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