9

I'm not new to C#, but I have found a behavior that is a little puzzling.

I have an interface

public interface IApplicationPage
{
    Person ThePerson { get; set;  }
    Application Application { get; set; }
}

I implement the interface on a page

public partial class tripapplication2 : System.Web.UI.Page, IApplicationPage
{
    Person IApplicationPage.ThePerson { get; set; }
    Application IApplicationPage.IApplicationPage.Application { get; set; }
}

However, when I attempt to reference ThePerson in the page itself I need to jump through hoops. For example.

1) ThePerson.Birthday

Gives an error saying "The name 'ThePerson' does not exist in the current context."

2) ((IMissionTripApplicationPage)this).ThePerson.Birthday

This works, but it looks awful.

Is there a better way to reference the implemented properties?

1
  • Can you show the specific code that doesn't work? Commented Jul 21, 2009 at 15:52

2 Answers 2

23

It looks like you left a line out in your sample. I believe the ThePerson line in the implementation should read

Person IApplicationPage.ThePerson { get; set; }

This type of implementation is known as an explicit interface implementation. This means the member will only be viewable when the object is seen through a reference of the interface type.

If you want the member to be viewable through a concrete type reference, make it public and remove the explicit implementation

public Person ThePerson { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

Aboslutely right, I did leave out the line! (careless copying). Thanks Jared.
3

Implement them as public properties:

public partial class tripapplication2 : System.Web.UI.Page, IApplicationPage
{
    public Person ThePerson { get; set; }
    public Application IApplicationPage.Application { get; set; }
}

Edit

Question now edited to show these were implemented originally as explicit. So, I should restate mine as implement them as public rather than explicit implementations of the properties.

2 Comments

You cannot make explicit implementations public (Application IApplicationPage.Application is explicit!)
It wasn't explicit when I answered. Quite right though of course.

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.