ANoob
ANoob

Reputation: 11

C# assignment not behaving as expected

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 
   }
}

Upvotes: 1

Views: 143

Answers (5)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391664

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);
   }
}

Upvotes: 1

Nick
Nick

Reputation: 5965

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.

Upvotes: 0

Joshua
Joshua

Reputation: 8212

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; }

Upvotes: 1

jro
jro

Reputation: 7480

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.

Upvotes: 0

Eilon
Eilon

Reputation: 25704

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).

Upvotes: 0

Related Questions