Why am I getting Cannot Convert from Method Group to Type error with this code?

1.8k views Asked by At

I am trying to migrate some legacy VB.Net code to c#. I am using the Telerik Code Converter.

Here is my legacy VB.Net Code

Public Shared Function GetEnumIntFromString(ByVal value As String, ByVal convertTo As [Enum]) As Integer
    Dim enumValueArray() As Integer

    Try
        If value.Trim.Length > 0 Then
            enumValueArray = [Enum].GetValues(convertTo.GetType)

            For Each i As Integer In enumValueArray
                If value.ToUpper = [Enum].GetName(convertTo.GetType, i).ToUpper Then
                    Return i
                End If
            Next
        End If

        'Return Integer.MinValue
        Throw New Exception("Invalid Enum value.  Cannot convert '" & value & "' to " & convertTo.GetType.ToString)
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try
End Function

And here is the output from Telerik/s code converter...

    public static int GetEnumIntFromString(string value, Enum convertTo)
    {
        int[] enumValueArray = null;

        try
        {
            if (value.Trim.Length > 0)
            {
                enumValueArray = Enum.GetValues(convertTo.GetType);

                foreach (int i in enumValueArray)
                {
                    if (value.ToUpper == Enum.GetName(convertTo.GetType, i).ToUpper)
                    {
                        return i;
                    }
                }
            }

            //Return Integer.MinValue
            throw new Exception("Invalid Enum value.  Cannot convert '" + value + "' to " + convertTo.GetType.ToString);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    //=======================================================
    //Service provided by Telerik (www.telerik.com)
    //Conversion powered by NRefactory.
    //Twitter: @telerik
    //Facebook: facebook.com/telerik
    //=======================================================

I am getting the Cannot convert from 'Method Group' to 'Type' error (red squiggle line) under converetTo.GetType. I tried adding parentheses after .GetType but that just gave me another error "Cannot implicitly convert type 'System.Aray' to 'int[]'"

The intent of the code is to provide an ENUM string value and the type of ENUM and get an integer value of the enum.

Any ideas would be greatly appreciated.

1

There are 1 answers

7
Camilo Terevinto On BEST ANSWER

I'd suggest you to stop right now using that code converter, it's missing a LOT of parenthesis:

  • value.Trim.Length => value.Trim().Length
  • convertTo.GetType => convertTo.GetType()
  • value.ToUpper => value.ToUpper()
  • Enum.GetName(convertTo.GetType, i).ToUpper => Enum.GetName(convertTo.GetType(), i).ToUpper()
  • convertTo.GetType.ToString => convertTo.GetType().ToString()

However, here is an easier way to do what you want:

public static int GetEnumIntFromString(string value, Enum convertTo)
{
    return (int)(Enum.Parse(convertTo.GetType(), value);
}