简体   繁体   中英

How to store multiple value types in a property?

I have an events class that I'm creating, that currently looks like the following:

public class SharePointOnErrorEventsArgs : EventArgs
{
    public SharePointOnErrorEventsArgs(string message, bool showException, Exception exception)
    {
        Message = message;
        Exception = exception;
        ShowException = showException;
    }

    /// <summary>
    /// Property to allow the storage of a more verbose and explainable error message
    /// </summary>
    public string Message { get; private set; }

    /// <summary>
    /// Object to store full exception information within
    /// </summary>
    public Exception Exception { get; private set; }

    /// <summary>
    /// Boolean value allows for verbose messages to be sent up the stack without
    /// the need for displaying a full exception object, or stack trace.
    /// </summary>
    public bool ShowException { get; private set; }
}

Now, instead of sending true or false for showException I'd like to send one of three values Debug , Info or Error - how can I tackle something like this? I don't really want to use a string as I want to always restrict this to one of those three values, but I'm unsure how to approach this when using properties.

You can use an enum:

public enum ShowExceptionLevel
{
    Debug,
    Info,
    Error
}

So your class will be:

public class SharePointOnErrorEventsArgs : EventArgs
{

    public enum ShowExceptionLevel
    {
        Debug,
        Info,
        Error
     }

    public SharePointOnErrorEventsArgs(string message, ShowExceptionLevel showExceptionLevel, Exception exception)
    {
        Message = message;
        Exception = exception;
        ShowException = showException;
    }

    /// <summary>
    /// Property to allow the storage of a more verbose and explainable error message
    /// </summary>
    public string Message { get; private set; }

    /// <summary>
    /// Object to store full exception information within
    /// </summary>
    public Exception Exception { get; private set; }

    /// <summary>
    /// Boolean value allows for verbose messages to be sent up the stack without
    /// the need for displaying a full exception object, or stack trace.
    /// </summary>
    public ShowExceptionLevel ShowException { get; private set; }
}

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