function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
scottnelsonsmithscottnelsonsmith 

Static methods cannot reference enums?

We're getting an unexpected error message when we try to compile this on the line noted below: "Error: Compile Error: Expression of type TestClass.TestType has no member named A at line ....."

 

If we comment out the contents of runTest then it compiles fine. We suspect a bug in Apex in which static methods cannot reference enums. This is ironic because static testMethods can!


@isTest
private class FixAccountIdInSalsTest
{
    private enum TestType{A, B}
    
    private static testMethod void testForA()
    {
        runTest(TestType.A); // COMPILES FINE
    }

    private static testMethod void testForB()
    {
        runTest(TestType.B); // COMPILES FINE
    }
    
    private static void runTest(TestType testType)
    {
        Test.StartTest();
        if (TestType.A == testType) // COMPILER BREAKS HERE
        {
            // do something
        }
        else if (TestType.B == testType) // AND PRESUMABLY BREAKS HERE
        {
            // do something else
        }
    }
}

Martha_SenetaMartha_Seneta
I experienced a similar issue, although my issue involved a class method rather than a static method.  I was able to resolve the issue by renaming the method parameter variable name of the enum to be different from the actual enum name.

For example, you could try updating the "runTest" method signature as follows, of course making sure to also update references to the "testType" variable within that method accordingly:

private static void runTest(TestType aType)
Jakub Lejtnar 4Jakub Lejtnar 4
Hi,
try to rename the variable from testType to something else so it differs from the enum name.
Kirill_YunussovKirill_Yunussov
Wow that is odd, but that fixed it.   
Mat JaggardMat Jaggard
So taking a case sensistive language and arbitrarily removing the case sensitivity is a bad idea. Who knew?!