<fr**@mayot.net a écrit dans le message de news:
11************* *********@h48g2 00...legr oups.com...
| 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
--
Joanna Carter [TeamB]
Consultant Software Engineer