Harley Pebley Harley Pebley - 2 months ago 6
C# Question

Why does the compiler give the error "The name 'identifier' does not exist in the current context"?

Here's the context (C#).

public class Base
{
public void SomeMethod()
{

}
}

public class Intermediate<TIntermediateTypeParam>
{

}

public class Derived<TDerivedTypeParam> : Intermediate<string>
{
public void SomethingElse()
{
SomeMethod(); // Error: The name 'SomeMethod' does not exist in the current context.
}
}


This was unexpected.
Any explanations as to why and how to fix it?

Answer

Intermediate doesn't subclass Base which is where the method is, therefore it is not part of the Derived class and Derived can't access it directly.

You would either need to call it from an instance of Base or have Intermediate (or Derived) subclass Base