2

I'm a C# newbie and am trying to implement an interface. I know I can't put access modiefiers onto interface methods so how do I get access to 'TestValue' in the public static 'Create' method of 'TestClass2' below? the error I get is...

'TestClass1' does not contain a definition for 'TestValue' and no extension method 'TestValue' accepting a first argument of type 'TestClass1' could be found

public interface IParent
{
    string TestValue { get; }
}

public class TestClass1 : IParent
{
    string IParent.TestValue
    {
        get { return "hello"; }
    }
}

public class TestClass2
{
    private string _testValue;

    public static TestClass2 Create(TestClass1 input)
    {
        TestClass2 output = new TestClass2();
        output._testValue = input.TestValue;
        return output;
    }
}
Community
  • 1
  • 1
Thundter
  • 582
  • 9
  • 22
  • You just can't put the modifiers on the _description_, but you can use them on the _implementation_. – phipsgabler Jul 12 '12 at 13:00
  • Not sure if your example here is simplified, but it may make sense for your static method to accept a parameter of type `IParent` instead of `TestClass1`. This would unintentionally fix your error also (I expect someone will explain why in the answers below). – Chris Shaffer Jul 12 '12 at 13:01
  • Just out of curiosity (don't ding me please!) - what is this line doing? private TestClass1 _testValue; I'm sorta a noob too, and it LOOKS TO ME like it's declaring an instance of itself within itself? Again, just curious, and it could be something i could use in the future =) – Losbear Jul 12 '12 at 13:01

3 Answers3

6

Add the public access modifier in your concrete implementation:

public class TestClass1 : IParent
{
    private TestClass1 _testValue; 

    public string TestValue
    {
        get { return "hello"; }
    }
}

EDIT: as you actually wrote an explicit interface implementation, I recommend you to see the following SO question: C# Interfaces. Implicit implementation versus Explicit implementation

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • At first I couldn;t get this to work but then I forgot to add the public access modifier. It works great now. Thanks for your help! – Thundter Jul 28 '12 at 12:15
0

You don't need access modifiers in the interface declaration because the point of an interface is to define the publicly accessible contract for any class implementing the interface. Essentially, although you don't specify an access modifier in the definition, all methods/properties are taken to be public.

This means that when you implement the interface, you're contractually bound to provide a public method/property that matches the interface's method/property signature.

In a nutshell, add the public access modifier to your concrete classes implementations and you'll be sweet.

Jason Larke
  • 5,289
  • 25
  • 28
0

I tried converting the object into the interface to get the solution like so

public class TestClass2
{
    private string _testValue;

    public static TestClass2 Create(TestClass1 input)
    {
        TestClass2 output = new TestClass2();
        output._testValue =  ((ITestInterface)input).TestValue;
        return output;
    }
}

This works too but I prefer the simpler solution.

Thundter
  • 582
  • 9
  • 22