473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Help with this sample

trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}
public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}
public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}

Nov 26 '06 #1
13 1939
In order to exhibit polymorphism, your classes Cls1, Cls2, Cls3 need to
all derive from a base class with your common method defined.

Declare an abstract base class with one method called Rtn(). Derive
your classes from this class and then during your foreach loop, call
the method on the base class rather than the concrete classes

eg

public abstract MyBaseClass
{
public abstract string Rtn();
}

public class Cls1 : MyBaseClass
{
public override string Rtn()
{
return "From class 1";
}
}
//repeat this for your other classes

when calling the method use

foreach (MyBaseClass c in coll)
{
Console.Writeln( c.Rtn() );
}

Hope this helps.

ps if you dont want to use an abstract class then you can just create a
concrete base class and mark the method as protected virtual

On Nov 26, 8:23 pm, "Praveen" <prav...@newsgroup.nospamwrote:
trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}

public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}

public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}
Nov 26 '06 #2
Hi,

Polymorphism exists in .NET via inheritance and interfaces. You haven't
illustrated the use of interfaces in your example, and you've barely
scratched the surface of inheritance. Here's a simple article that you may
want to read:

"Polymorphism in object-oriented programming"
http://en.wikipedia.org/wiki/Polymor...ed_programming
Cls3 y = new Cls3();
Your code won't compile because the line above attempts to redefine "y".
foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
You can't call obj.Rtn() because obj is of the Type, "System.Object", which
doesn't provide a method named, "Rtn". You must cast "obj" to a Type that
has a method named, "Rtn":

foreach (object obj in coll)
{
if (obj is Cls1)
Console.WriteLine(((Cls1) obj).Rtn());
else if (obj is Cls2)
Console.WriteLine(((Cls2) obj).Rtn());
else if (obj is Cls3)
Console.WriteLine(((Cls3) obj).Rtn());
}

Obviously, this isn't very dynamic at all.

Since you'd like to late-bind to an instance of Cls1, Cls2 and Cls3 to call
"Rtn", a method which each of these classes has in common with the same
signature, you could use an interface:

interface IReturn
{
string Rtn();
}

class Cls1 : IReturn
{
public string Rtn() { return "From Cls1"; }
}

class Cls2 : IReturn
{
public string Rtn() { return "From Cls2"; }
}

....

And you can use the interface as such:

IReturn[] returningColl = new IReturn[] {
new Cls1(), new Cls2()
};

foreach (IReturn returningObject in returningColl)
Console.WriteLine(returningObject.Rtn());

--
Dave Sexton

"Praveen" <pr*****@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}
public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}
public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}

Nov 26 '06 #3
Hi Praveen,

You cannot invoke the Rtn method from the variable of type object without
casting it to the appropriate class type.
But since you want to use polymorphism, you dont want to cast it.

polymorphism can be applied in this case, can be accomplished in the
following ways

--Derive Cls1, Cls2 and Cls3 from a common base type.
The base type should have a virtual method Rtn which you should override
in each of the classes Cls1, Cls2 amd Cls3.

Example:
class BaseType
{
protected virtual string Rtn()
{
return String.Empty;
}

}
class Cls1 : BaseType
{
protected override string Rtn()
{
return "From Cls1";;
}
}
Use BaseType[] instead of object[]

OR

--Create a particular Interface which defines the method Rtn.
Implement the Rtn method in each of the classes Cls1, Cls2 amd Cls3.
interface IBaseType
{
string Rtn();
}
class Cls1 : IBaseType
{
public string Rtn()
{
return "From Cls1";;
}
}
Use IBaseType[] instead of object[]

Regards,
Hameer Saleem
"Praveen" wrote:
trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}
public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}
public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}

Nov 26 '06 #4
Praveen... Polymorphism, having more than one form, can be implemented
using
interfaces, abstract classes or events. Whenever I see a long switch in
code, I
start thinking about a polymorphic solution. This tutorial may help:

http://www.geocities.com/jeff_louie/oop.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #5
Hi Jeff,

I don't think that "events" have anything to do with "implementing"
polymorphism.

--
Dave Sexton

"Jeff Louie" <an*******@devdex.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Praveen... Polymorphism, having more than one form, can be implemented
using
interfaces, abstract classes or events. Whenever I see a long switch in
code, I
start thinking about a polymorphic solution. This tutorial may help:

http://www.geocities.com/jeff_louie/oop.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***

Nov 26 '06 #6
Hi Dave... IMHO, events/delegates can be looked at as type safe pointers
to
polymorphic methods.

http://www.geocities.com/jeff_louie/oop31.htm
http://msdn2.microsoft.com/en-us/library/ms173173.aspx

It took me a while to understand this, so that is why I am sticking my
neck out
here to so state this.

Regards,
Jeff
>I don't think that "events" have anything to do with "implementing"
polymorphism.<

*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #7
Hi Jeff,

I'll agree with you that delegates implement polymorphism since they can be
late-bound to their targets, but not events. Events must be part of an
interface, which is already polymorphic in nature. It's the delegates
registered with the events that are polymorphic, not the event itself.

--
Dave Sexton

"Jeff Louie" <an*******@devdex.comwrote in message
news:OZ**************@TK2MSFTNGP02.phx.gbl...
Hi Dave... IMHO, events/delegates can be looked at as type safe pointers
to
polymorphic methods.

http://www.geocities.com/jeff_louie/oop31.htm
http://msdn2.microsoft.com/en-us/library/ms173173.aspx

It took me a while to understand this, so that is why I am sticking my
neck out
here to so state this.

Regards,
Jeff
>>I don't think that "events" have anything to do with "implementing"
polymorphism.<

*** Sent via Developersdex http://www.developersdex.com ***

Nov 26 '06 #8
Hi Dave.. This is how I see it. Events and Delegates are intertwined in
NET. In
general they are often used in tandom. Delegates are type safe pointers
to a
polymorphic method. Events are used to simplify the use of Delegates.
When
you fire an event you are calling one or more polymorphic methods. This
type of
semantic debate is important but clouds the concept that polymorphism is
not
limited to interfaces and abstract classes in .NET.

I am trying to say that you can implement polymorphic behaviour in .NET
using
events _and delegates_. To some, this idea is new and may be an "aha"
moment.
I apologize if I was not clear in my original post.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #9
Hi Jeff,
When
you fire an event you are calling one or more polymorphic methods. This
type of
semantic debate is important but clouds the concept that polymorphism is
not
limited to interfaces and abstract classes in .NET.
I disagree.

My point is that delegates are providing the polymorphism, not the events.
Events are just a construct that aggregates delegates for private
invocation.

When you write, "you can implement polymorphic behaviour in .NET using
events _and delegates_.", I disagree that "events" have anything to do with
it. Delegates being added to an "event" are polymorphic regardless. Adding
delegates to an event does not provide any further polymorphic aspects to
the already late-bound delegates. An event itself, being part of a class
definition or interface, may be polymorphic in the sense that it's
implemented or inherited, just like a public property. Public properties
alone are not polymorphic and neither are events.

I think the key here is that Typed delegates derive from System.Delegate,
which is why delegates are polymorphic - inheritance. Events are not
objects, so they cannot be polymorphic.

To say that you can use events to implement polymorphism is like saying you
can use public properties to implement polymorphism, when in fact it's
actually inheritance and interfaces that are required for polymorphism, not
the class members.

--
Dave Sexton

"Jeff Louie" <an*******@devdex.comwrote in message
news:uQ**************@TK2MSFTNGP03.phx.gbl...
Hi Dave.. This is how I see it. Events and Delegates are intertwined in
NET. In
general they are often used in tandom. Delegates are type safe pointers
to a
polymorphic method. Events are used to simplify the use of Delegates.
When
you fire an event you are calling one or more polymorphic methods. This
type of
semantic debate is important but clouds the concept that polymorphism is
not
limited to interfaces and abstract classes in .NET.

I am trying to say that you can implement polymorphic behaviour in .NET
using
events _and delegates_. To some, this idea is new and may be an "aha"
moment.
I apologize if I was not clear in my original post.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***

Nov 26 '06 #10
Dave... I never said events themselves were polymorphic. I just
finished
saying that you could implement polymorphic behavior using events and
delegates. I added the "and delegates" in response to your post as I
thought
you had a valid point, that I was not being absolutely clear. Here is a
definition of Implement:

Main Entry: 2im·ple·ment
Pronunciation: -"ment
Function: transitive verb
1 : CARRY OUT, ACCOMPLISH; especially : to give practical effect to and
ensure of actual fulfillment by concrete measures
2 : to provide instruments or means of expression for

Are you arguing that events are not related to delegates? I think that
they are
very much related to delegates.

"Events use delegates to provide type-safe encapsulation of the methods
that
will be called when triggered. A delegate can encapsulate both Named
Methods and Anonymous Methods."

IMHO events and delegates are tightly related and together provide a
convenient set of instruments and means for polymorphic behavior in
..NET.
Sometimes I tire of these semantic debates. The concept that
polymorphism
can be accomplished beyond the classical interface and abstract class is
what
is important, not these arguments. I am begining to wonder if it is even
worth
the effort to try to contribute ideas to this forum.
Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #11
Hi Jeff,

<snip>
Are you arguing that events are not related to delegates? I think that
they are
very much related to delegates.
No.

I'm suggesting that defining an event on a class is not implementing
polymorphism, although it may be implementing an interface, which is one way
to implement polymorphism. Events and delegates cannot be used to implement
polymorphism. Polymorphism is implemented through the use of inheritance or
interfaces. For example, delegates are polymorphic through inheritance. If
delegates didn't share a base class then there would be no interface on
which to invoke the target method, and delegates wouldn't be of much use.
But defining a Typed delegate, with or without the use of an event, is not
"implementing" polymorphism.

Maybe you should cite the definition of polymorphism instead :)

<snip>
IMHO events and delegates are tightly related and together provide a
convenient set of instruments and means for polymorphic behavior in
NET.
Sometimes I tire of these semantic debates. The concept that
polymorphism
can be accomplished beyond the classical interface and abstract class is
what
is important, not these arguments. I am begining to wonder if it is even
worth
the effort to try to contribute ideas to this forum
What's important is that people understand what polymorphism means so that
it will be clear what others are talking about when the word comes up in
conversation or print. If we can't agree on the definition of polymorphism,
then the ambiguity created by our competing definitions of the word will
render it useless.

If you think I'm being too closed-minded then you might want to provide a
more compelling argument than just citing the definition of "implement" :)

--
Dave Sexton
Nov 26 '06 #12
Dave... I never said events themselves were polymorphic. I just
finished
saying that you could implement polymorphic behavior using events and
delegates. I added the "and delegates" in response to your post as I
thought
you had a valid point, that I was not being absolutely clear. Here is a
definition of Implement:

Main Entry: 2im·ple·ment
Pronunciation: -"ment
Function: transitive verb
1 : CARRY OUT, ACCOMPLISH; especially : to give practical effect to and
ensure of actual fulfillment by concrete measures
2 : to provide instruments or means of expression for

Are you arguing that events are not related to delegates? I think that
they are
very much related to delegates.

"Events use delegates to provide type-safe encapsulation of the methods
that
will be called when triggered. A delegate can encapsulate both Named
Methods and Anonymous Methods."

IMHO events and delegates are tightly related and together provide a
convenient set of instruments and means for polymorphic behavior in
.NET.
Sometimes I tire of these semantic debates. The concept that
polymorphism
can be accomplished beyond the classical interface and abstract class is
what
is important, not these arguments. I am begining to wonder if it is even
worth
the effort to try to contribute ideas to this forum.
Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #13
Dave... I give up.

Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
Nov 26 '06 #14

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

Similar topics

16
by: Henri Schomäcker | last post by:
Hi folks, I am developing a apache2 so module in c++. At the moment, I'm trying to get it to compile with automake & friends, but don't get it to work. I tried to modify the example in the...
5
by: franklini | last post by:
i cant seem to figure out what is wrong with this class. it has to do with the input/output stream override. please can somebody help me. #include <iostream> #include <string> #include <vector>...
1
by: jase_dukerider | last post by:
Hi I have an assignment to hand in shortly for which I am after some guidance. The task is to read a WAV file, request a fade in /out time for the track from the user and the do the fade by...
0
by: Henry Reardon | last post by:
I just installed DB2 Personal Developer's Edition V8.2 (FP7) over my old DB2 V7.2 instance but I'm having some problems. Can anyone help? The first problem is that my migration didn't go too...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
11
by: Bryan Kyle | last post by:
Hi All, I'm fairly new to C# and Generics and I'm wondering if anyone has some suggestions for me. I'm trying to implement a simple DAO framework using generics to keep my code as clean as I...
0
by: Hennie Coertze | last post by:
Good day, My knowledge of XML is next to none and I only have one XSL code to use. I also assume I may be using incorrect jargon and hope you will understand what I need. I have an XSL style...
3
by: buntyindia | last post by:
Hi I am creatng a Scrollable Table following is my code. This table is working fine in Mozilla but in IE 6 it is not showing the Scroll Bars.. Please Hellp... Regards, <html>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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,...
0
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...

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.