Share via


Returning enum from method

Question

Sunday, January 10, 2016 7:47 PM

I'm trying to pass an int to a method and want to return an enum from that method.  Something like the code below:

        public HTML_REPORT_RUN GetReportRun(int runID)
        {
                if (runID ==  (int)HTML_REPORT_RUN.ABC)
                    HTML_REPORT_RUN.ABC;
                if (runID ==  (int)HTML_REPORT_RUN.DEF)
                    return HTML_REPORT_RUN.DEF
        }

How do I do something like that?

All replies (1)

Sunday, January 10, 2016 8:06 PM âś…Answered

at the simplest, just do this:

        public HTML_REPORT_RUN GetReportRun(int runID){
            return (HTML_REPORT_RUN)runID;
        }

if it's possible values not in the enum may get passed in, you may want to do some value testing first:

http://john-thiriet.com/2012/03/back-to-basics-csharp-casting-an-integer-into-an-enumeration/

https://msdn.microsoft.com/en-us/library/essfb559%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396