Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store multiple value types in a property?

Tags:

c#

.net

enums

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.

like image 611
Michael A Avatar asked Dec 19 '22 23:12

Michael A


1 Answers

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; }
}
like image 140
Szymon Avatar answered Dec 28 '22 10:12

Szymon