Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Property, interface, inheritance and cast

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fred@mayot.net - 09 Nov 2006 10:21 GMT
Hi,
Why should I do a cast (IOfferSetter) in the constructor of the Test
class?
Thanks.
Fred

   public interface IOfferSetter { object Offer { set;}  }

   public class OfferCtx
   {
       protected object offer;
       public object Offer { get { return offer; } }
   }

   public class OfferCtx2 : OfferCtx, IOfferSetter
   {
       object IOfferSetter.Offer
       {
           set { offer = value; }
       }
   }

   public class Test
   {
       public Test()
       {
           OfferCtx2 ctx = new OfferCtx2();
           ((IOfferSetter)ctx).Offer = 10;
           object off = ctx.Offer;
       }
   }
Joanna Carter [TeamB] - 09 Nov 2006 10:51 GMT
| Why should I do a cast (IOfferSetter) in the constructor of the Test
| class?

Because you have used "explicit interface implementation" to implement the
Offer property of IOfferSetter.

This means that the Offer property is private when viewed from an OfferCtx2
instance, only becoming visible when viewed through an IOfferSetter
interface reference.

If you want to access the property from an OfferCtx2 reference, then you
need to declare the property using "implicit interface implementation"

public interface IOfferSetter { object Offer { set;}  }

public class OfferCtx
{
 private object offer;

 protected void SetOffer(object value)
 {
   offer = value;
 }

 public object Offer
 {
   get { return offer; }
 }
}

Don't use protected fields always use at least a protected method.

public class OfferCtx2 : OfferCtx, IOfferSetter
{
 public new object Offer
 {
   get { return base.Offer; }
   set { offer = value; }
 }
}

This re-declaration of the Offer property will cause a compiler warning that
the base property will be hidden, thus the need for "new" in the
declaration.

... then you can do this :

public class Test
{
 public Test()
 {
   OfferCtx2 ctx = new OfferCtx2();
   ctx.Offer = 10;
   object off = ctx.Offer;
 }
}

Finally, if you intend to store numbers in the Offer property, don't declare
the field/property as object; this will incur a boxing/unboxing speed
penalty. Use a type appropriate to the value being stored.

Joanna

Signature

Joanna Carter [TeamB]
Consultant Software Engineer

fred@mayot.net - 09 Nov 2006 13:51 GMT
Thanks for your advise.
The problem is that I didn't want to use the "new" keyword because you
must always write code for the getter implementation (even if it's just
a base call), which is error prone (In our case, we will have many many
classes that will use the getter and very few that will use the
setter). What I would have liked to write is the following

public interface IOfferSetter { object Offer { set; }  }

public class OfferCtx
{
  public object Offer { get { /* code */ } }
}
public class OfferCtx2 : OfferCtx, IOfferSetter
{
  public object Offer
  {
     set { /* code */ }
  }
}

It does not compile and that's why I cannot use the implicit interface
implementation.
I was just wondering why the compiler complains about the
OfferCtx2.Offer property hiding the inherited one. In this case, there
is no ambiguity. It would hide something if a getter was defined in
OfferCtx2.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.