1

Have a class that is derived from System.Windows.Forms.UserControl and implements an interface IFoo. After having a SomeControl instance created with a Height specified and having that assigned to a local IFoo variable display, an attempt to assign a value to display's Height property via it's public setter isn't working for me.

I'm observing this while stepping through the debugger so I've trumped up this test case to simplify thing. I realize "select isn't broken" so there is a gap in my knowledge here of why I can't set this property so I'd like to understand what that is. Thanks.

public interface IFoo
{
    int Height {get;set;} // which is implemented by UserControl
}

public class SomeControl : UserControl, IFoo { /*impl goes here*/ }

[TestFixture]
public class TestFixture
{
   [Test]
   public void Test()
   {
       IFoo display = ...
       // assume that display is of type SomeControl 
       // and already has a value for Height at 123

       Assert.IsTrue(display.Height == 123);
       display.Height = 789; 
       Assert.IsTrue(display.Height == 789);  //FAILS 
   }
}
4
  • So there's no Exception when you try to set your Height property? For the second Assert statement, is the value still 123, or something else? Commented Jan 4, 2010 at 22:09
  • Questions: Have you added any code to handle the Height property yourself, overriding/replacing the existing Height property of UserControl? Is the user control docked on something so that its height really won't change at all? Commented Jan 4, 2010 at 22:18
  • It would help if you posted a complete, but short, compilable and executable/testable program so that we could reproduce the problem ourselves. Commented Jan 4, 2010 at 22:20
  • Practice the debugger's Set Breakpoint and SingleStep commands. Commented Jan 4, 2010 at 22:28

5 Answers 5

1

It's because the UserControl already has a height property defined. To access your implementation you'll need to cast it.

((IFoo)display).Height = 789;

That should work. Also, I assume that your property of height is explicitly defined?

public int IFoo.Height { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

1

The following code works fine, so the problem must be in your assumptions or in the code you've left out.

Can you please post a short, but complete, program that we can compile and test ourselves?

using NUnit.Framework;
using System.Windows.Forms;
public interface IFoo
{
    int Height { get; set; } // which is implemented by UserControl
}

public class SomeControl : UserControl, IFoo
{
    public SomeControl()
    {
        Height = 123;
    }
}

[TestFixture]
public class TestFixture
{
    [Test]
    public void Test()
   {
       IFoo display = new SomeControl();

       Assert.IsTrue(display.Height == 123);
       display.Height = 789; 
       Assert.IsTrue(display.Height == 789);
   }
}

Comments

0

Is the type that implements IFoo a struct or a class? If it's a struct then you can't change its properties after you create it.

If you share the code for SomeControl that should help diagnose the problem further.

Also, if you could run the unit test in the debugger and inspect the value, that would be helpful as well.

Lastly, the recommended way to write such tests is as follows:

Assert.AreEqual(123, display.Height);

This will provide much better error reporting when the unit test fails (which it is).

1 Comment

Or using the syntax helper Assert.That(display.Height, Is.EqualTo(123)). Makes it easier to use the correct expected and actual values.
0

An interface in C# is merely a contractual description of the requirements for any implementors. Key to that phrase is "implementation".

EDIT: just saw that your class inherits from UserControl, which already holds a Height property. To access your interface's Height property, you must explicitly reference that property from your test.

1 Comment

Just an FYI, Auto-Implemented Properties (blah {get; set;}) are a .Net 3.0+ feature. You can get them to work in 2.0 by using the latest compiler. (Visual Studio 2008 does this but Visual Studio 2005 cannot)
0

I think your issue is that UserControl already has a property named Height, and that IFoo also defines Height. I don't think you've provided enough information to answer the question, but I would assume, that depending on your implementation of IFoo, your Height property is either hiding the UserControl version of Height, or is being hidden by it. I believe it is the latter, because my recollection is that Height is a read only property on UserControl.

2 Comments

Interface declarations don't hide implementations, because they aren't implementations, just definitions
The interface itself wouldn't, but the implementation might. It all depends on how the class was written. For instance, was there an explicit implementation of the interface method, or an implicit one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.