473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

polymorphism/boxing question

I've run into a problem in some code involving two class hierarchies
that I can't figure out: I have an event hierachy topped with an
interface, let's say-

IEvent,
EventA : IEvent,
EventB : IEvent

And some handlers-

abstract EventHandler
EventHandler1 : EventHandler

Now in the abstract EventHandler class I have a method which processes
events -

virtual void ProcessEvent(IE vent e) {}

and a loop which calls that method for every event I put on the
handler object (it runs in its own thread, but that's irrelevant). I
want the classes to process events like this:

class EventHandler1 : EventHandler
{
protected override void ProcessEvent(IE vent e)
{
//do some default processing in case there is no specific handler
}

protected void ProcessEvent(Ev entA e)
{
//specific processing for EventA type events
}

protected void ProcessEvent(Ev entB e)
{
//specific processing for EventB type events
}
}

So when the EventHandler method tries to process an event, it goes to
the ProcessEvent method for the most derived IEvent class for the
event object being passed in. What happens as it is is that the
ProcessEvent(IE vent) method is called, presumably because the event
object is boxed as an IEvent where it is held in the superclass.

I was kind of hoping that the runtime would just know what type the
event was, and call the appropriate method for that class. It doesn't
though, so is there a) some combination of language features such as
virtual / abstract / override that i can employ to achieve this, or b)
some way to dynamically unbox the event in the superclass? I looked
into dynamic casting and it doesn't seem to be possible in c#. But the
object knows what it is so it should be possible to extract it out of
its IEvent box and invoke an appropriate method through normal
polymorphism right?

Feb 20 '07 #1
3 1866
quick update by self:
after looking into it it almost seems like this is impossible as I
read that C# determines which overload to invoke for a given call at
compile time. This seems to me to be lame, and kind of hobbles
polymorphism doesn't it? At least I should be able to unbox my
variable from its generic interface type and have the call made on it
then.

Feb 20 '07 #2
On Feb 20, 12:09 pm, loren...@gmail. com wrote:
I've run into a problem in some code involving two class hierarchies
that I can't figure out: I have an event hierachy topped with an
interface, let's say-

IEvent,
EventA : IEvent,
EventB : IEvent

And some handlers-

abstract EventHandler
EventHandler1 : EventHandler

Now in the abstract EventHandler class I have a method which processes
events -

virtual void ProcessEvent(IE vent e) {}

and a loop which calls that method for every event I put on the
handler object (it runs in its own thread, but that's irrelevant). I
want the classes to process events like this:

class EventHandler1 : EventHandler
{
protected override void ProcessEvent(IE vent e)
{
//do some default processing in case there is no specific handler
}

protected void ProcessEvent(Ev entA e)
{
//specific processing for EventA type events
}

protected void ProcessEvent(Ev entB e)
{
//specific processing for EventB type events
}

}

So when the EventHandler method tries to process an event, it goes to
the ProcessEvent method for the most derived IEvent class for the
event object being passed in. What happens as it is is that the
ProcessEvent(IE vent) method is called, presumably because the event
object is boxed as an IEvent where it is held in the superclass.

I was kind of hoping that the runtime would just know what type the
event was, and call the appropriate method for that class. It doesn't
though, so is there a) some combination of language features such as
virtual / abstract / override that i can employ to achieve this, or b)
some way to dynamically unbox the event in the superclass? I looked
into dynamic casting and it doesn't seem to be possible in c#. But the
object knows what it is so it should be possible to extract it out of
its IEvent box and invoke an appropriate method through normal
polymorphism right?
Is there a reason why you can't put ProcessEvent within the IEvent
interface itself? Then you _could_ use polymorphism to handle the
different types of events. It also has the advantage that every time
you implement IEvent in another class you don't have to add a method
to your central event handler to handle the new case: it's handled in
the class itself.

Feb 21 '07 #3
<lo******@gmail .comwrote:
quick update by self:
after looking into it it almost seems like this is impossible as I
read that C# determines which overload to invoke for a given call at
compile time. This seems to me to be lame, and kind of hobbles
polymorphism doesn't it? At least I should be able to unbox my
variable from its generic interface type and have the call made on it
then.
Okay, to start with: there's no boxing involved in your examples.
Boxing is a matter of representing a value type value as a reference
type instance. You haven't shown any value types being involved at all.

And yes, you're right: overloading is performed at compile time. That
doesn't hobble polymorphism, however, which is more about *overriding*
than overloading.

"Double dispatch" may be the answer to your issues, however. See
http://en.wikipedia.org/wiki/Double_dispatch

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 22 '07 #4

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

Similar topics

37
2854
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
14
323
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
3
2149
by: Marshal | last post by:
This is a definite and reproduceable bug in Web Services, Visual Studio 2003. Here is a simple example. Create a web service... // this represents an original version of an object class ObjectV1 { ... } // holds some arbitrary data // this represents a newer version with extra information class ObjectV2 : ObjectV1 { ... } // adds some additional information
5
1764
by: Craig | last post by:
Hi everyone, As a relative new-comer to the wonderful world of .NET Framework and the C# langauge I have come across something that I would like to clarify (hopefully with the help of kind people such as yourselves). To quote, "Everything in C# is an object" (including "primitive" value types, primitive being in quotations because this is what my question concerns).
1
2228
by: Tom | last post by:
Couple of questions relating to boxing. Firstly, I already know that boxing is the processing of temporarily copying a ValueType (e.g. struct, enum) to the heap so that the system can treat a value type like a reference type. However, I have some questions relating to implicit boxing: 1. If I add custom instance method on a struct, will it box that type each time the method is called? For example, suppose I have the following: public...
18
3865
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two different objects to respond to the same request message in their own unique way" I thought that it was: "the ability of same object to respond to different messages in
94
5696
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. Thanks.
19
13731
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
7881
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every other platform in every other language. Normally a value type is the actual data itself stored in...
0
10457
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
10231
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...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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
9054
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...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
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
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.