Don't panic!

 

Cry about...
MS-Windows Troubleshooting


Property, indexer, or event 'XYZ' is not supported by the language; try directly calling accessor method ...


Symptom:

When trying to compile C# code which uses a library written in VB.Net the following compiler error is generated:

Property, indexer, or event 'SomeName' is not supported by the language; try directly calling accessor method 'SomeNamespace.SomeClass.get_SomeName(...)

where "SomeName" is the name of a property that you are using in the C# code, "SomeClass" is the name of the class which contains that property and "SomeNamespace"  is the namespace which contains the class.

The property (or function) in VB.Net is defined with one or more optional parameters.

For example, calling the following VB.Net property:

Public Readonly Property NextLine(Optional ByVal consume as Boolean = False) as String
    Get
        NextLine = m_nextLine
        if (consume) then MoveNext
    End Get
End Property

from the following C#:

string line = input.NextLine;

will cause the following error to be generated:

Property, indexer, or event 'NextLine' is not supported by the language; try directly calling accessor method 'ExampleNamespace.ExampleClass.get_NextLine(bool)

Cause

VB.Net allows functions and properties to be defined which take optional parameters, but C# does not support this. (Although I understand that C# 4.0 will support them.) Because C# does not support optional parameters the compiler generates this error.

Possible Remedies

  • The error can be avoided by calling the accessor property directly and with all the arguments explicitly provided.

    For example, modify the C# shown above to:

    string line = input.get_NextLine(false);

  • Alternatively, rewrite the VB.Net property to remove the optional argument. Functionality roughly equivalent to that provided by optional arguments can be achieved using overloads, where a different overloaded function is provided for each combination of arguments.

    For example, rewriting the VB.Net show above to:

    Public Overloads Readonly Property NextLine(ByVal consume as Boolean = False) as String
        Get
            NextLine = m_nextLine
            if (consume) then MoveNext
        End Get
    End Property
    Public Readonly Property NextLine() as String
        Get
            NextLine = m_nextLine
        End Get
    End property

    This way you can still call the "NextLine" property from C# without arguments, but you will still need to call the version with arguments via the "accessor" method (above).


These notes have been tested within Microsoft Visual Studio .NET 2008, and may apply to other versions as well.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.