tags:

views:

426

answers:

3

The following line of code presents the title error:

ITestClass t = new TestClass();

TestClass implementation:

    public class TestClass : ITestClass {


  public static TestClass Instance
        {
            get
            {
                return TestClass.Instance;
            }
        }

 //Interface members

}

I have another set of classes with similiar interfaces where this cast works, why doesn't it work here?

+4  A: 

Most likely the Interface that you are trying to assign do isn't part of the class that you are newing. Double check your interface implementations for mistakes, this is a completely valid piece of code.

If you have a specific error, provide code that produces the same error, the code you provided does not error when compiled.

joshperry
@joshperry, you are correct the interface I was trying to assign to didn't exist where I thought it did
CodeMonkey
"Valid"? I dunno if I'd go that far, given the infinite recursion in the Instance getter. It certainly compiles; good luck getting it to run.
Randolpho
This question was about compiling, I answered the question. I'm not in the business of reviewing test code for semantic accuracy, especially when it doesn't exhibit the error that was the focus of the question.
joshperry
+4  A: 

I'm not sure about the error you put but the property you listed is a bug. The getter just returns the property and is hence infinitely recursive. This will cause a stack overflow at runtime if you attempt to access it.

JaredPar
Wow, I totally missed that one :)
cwap
A: 

The only possibility for such an error is that TestClass or ITestClass doesn't mean the same thing in one case and another.

The most common cause for this is to have the assembly containing TestClass and your main assembly reference 2 different dll files both with an interface named ITestClass.

VirtualBlackFox