Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Static class with Enum

I have a tricky question that has been befuddling me for a while. I have the following code declaration...

namespace ESEGURCI.WEB.BusinessLogicLayer.Commons
{
    public static class ParameterUtilities
    {
        public enum ParameterEnum
        {
            MAX_LOGIN_ATTEMPTS,
            AUDIT_MODIFICATIONS
        }
    }
}

and I call the code like so "ParameterUtilities.ParameterEnum.MAX_LOGIN_ATTEMPTS" Problem is once every full moon I get the error "object reference not set to an instance of an object" on this line... It's like the code only works 99.9% of the time...

The only thing that occurs to me is that since the enum is a value type that there can be a chance that the enum is null when the static class is called... But I can't find any documentation on this behavior...

Can someone enlighten me why this happens? I know I should probably remove the enum out of the static class, and declare the enum as standalone but I'd like to know why this is happening first...

Thanks, S

Update

Ok, to everyone who asked for more code, the following is the full function where the error occurs...

    public static int GetPageSize(int companyId)
    {
        int pageSize = 0;

        // error happens bellow this line

        ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter parameter = ESEGURCI.WEB.BusinessLogicLayer.Entities.Parameter.GetParameter(ParameterUtilities.ParameterEnum.AUDIT_MODIFICATIONS.ToString(), companyId);

        // error happens above this line

        int.TryParse(parameter.Value, out pageSize);

        return pageSize;
    }
like image 398
bastos.sergio Avatar asked Mar 01 '26 13:03

bastos.sergio


2 Answers

ParameterUtilities.ParameterEnum.MAX_LOGIN_ATTEMPTS won't ever throw a null reference exception, no matter what the Moon looks like. The error is probably triggered by an other instruction on the same line (assignment to a variable?).

like image 95
Kevin Gosse Avatar answered Mar 04 '26 01:03

Kevin Gosse


An enum can't be null.

Split up the line as in the listing below and see which statement throws the exception. I bet it happens somewhere in Parameter.GetParameter():

using ESEGURCI.WEB.BusinessLogicLayer.Entities;

// ...

var auditModifications = 
    ParameterUtilities.ParameterEnum.AUDIT_MODIFICATIONS.ToString();
var parameter = Parameter.GetParameter(auditModifications, companyId);
like image 38
Dennis Traub Avatar answered Mar 04 '26 03:03

Dennis Traub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!