473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Property, interface, inheritance and cast

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.Of fer
{
set { offer = value; }
}
}

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

Nov 9 '06 #1
2 1758
<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
Nov 9 '06 #2
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.

Nov 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4875
by: Martin | last post by:
Hi, Suppose I have 3 interfaces : IStats IEngine ICreateCar each derives from IUnknown
16
25404
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
0
1332
by: news_mail_jpa | last post by:
Hi! I'd like to implement a private interface and I have the choices mentioned in the subject. The implementation using private inheritance is probably cleaner but it also adds some space and time overhead compared to the 'cast' -method. The problem with the cast method is that I don't know whether its C++ standard-compliant and/or portable? Example:
7
3662
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b; but it complains for the following lines
2
2507
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. To see what I mean look at the example below where the class implementing the interface actually wants the event to be for a more specific delegate. Now this seems to work but the code, to me is unnecessarily ugly. This leaves me wondering if...
15
26261
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. Why would someone do the following and what does it mean?
9
1479
by: Sean Chambers | last post by:
Not sure if this is the correct way to go about this, but it seems correct. I'm trying to eliminate dependencies through interfaces and want to minimize casting as much as possible, as a result I have interface inheritance setup like so Here's my class setup: BlogPost : IBlogPost PodcastPost : BlogPost, IPodcastPost
6
9147
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
8
1368
by: Skysurfer | last post by:
Hi. I have some control of different type which have a particolar property, for instance MyProp. Some other controls don't have this property. How can i make a generic sub that set this property without cast? I can test if the control has the property with this line: Control.GetType.GetProperty("MyProp") But... how can i set this property?
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.