Author Topic: Polymorphism and Inheritance  (Read 2506 times)

0 Members and 1 Guest are viewing this topic.

dukesys

  • Guest
Polymorphism and Inheritance
« on: January 03, 2007, 06:48:06 PM »
Hello All,

I have a project I am currently working on using VB .NET and VS 2005. Basically one of my classes maintains a collection of various objects all derived from a base class, but different. I want each of these derived classes to have a common method, but the paramaters for the method will vary.

Example, here is a cut down version of the base class and derived classes and what I want them to do.

Class clsString
public mustoveride sub AddPoint(index as integer, pnt as clsPnt)
End class

Here are some versions of the derived classes.

Class clsString3D
inherits clsString
public overrides sub AddPoint(index as integer, pnt as cls3d)
'do some other stuff here
End class

Class clsString6D
inherits clsString
public overrides sub AddPoint(index as integer, pnt as cls6d)
'do some other stuff here
End class

My program returns the object in this manner
Public function getString(byref name as string) as clsstring
getstring=string.item(name)
end function

I then call the function like this
Dim STR as clsstring
dim P6 as new cls6D
p6.setvals(5,7,34.6,45,3)
str=MDL.getString("MC01") 'this would in a working version return a string of clString6D
Str.AddPoint(4, P6)

Now I realise that you cannot override the function if the function signature is not the same, but shadowing doesn't seem to work either. Could someone please explain to me correct way for me to code what I want to do, or even if it is possible.


Regards,

Martin Duke

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Polymorphism and Inheritance
« Reply #1 on: January 04, 2007, 05:01:45 PM »
It's hard to say what's best without seeing a bit more of your design. 

Could the objects you're passing as the second argument to AddPoint() derive from a common base?

Could you wrap both arguments into a single parameter object? http://www.refactoring.com/catalog/introduceParameterObject.html

Those are a couple of ideas off the top of my head.
Bobby C. Jones

Glenn R

  • Guest
Re: Polymorphism and Inheritance
« Reply #2 on: January 05, 2007, 08:07:33 AM »
In Lucerne at the moment, so I don't have all my toys with me, however, this strikes me as strange:

Quote
I realise that you cannot override the function if the function signature is not the same

Unless you meant something else, that's not true - the function signature is made up of the return value, the name and the arguments. Changing the return value is not enough - you must change the number and type of arguments to OVERLOAD (which is what I think you meant Martin) a function.

Now if you actually DID mean "override" a base clase class function in a derived class, that would mean the function signature would have to be exact...hence you can override it...hope that makes sense. I'm tired from all this holidaying :)

Have you thought about using INTERFACES instead? They might give you the behaviour you desire as you can test and cast an object for a particular interface.

Just some rambles.

Actually, after reading your post again Martin, you could in your collection class, try to cast to particular objects. If it works, call the function with the required number of arguments...a big SWITCH/SELECT case statement perhaps.

Maybe you need to redesign your class heirarchy a tad...

Cheers,
Glenn.

dukesys

  • Guest
Re: Polymorphism and Inheritance
« Reply #3 on: January 09, 2007, 08:53:01 PM »
Hello ,

Thank you both your feedback the Bobby was on the right track as the second argument (pnt as cls3D, cls6D etc) all derives from a common base class and so by relying onthe base class clsPnt instead of the inherited class the system now works.

Glenn:I was talking about Overriding as what I wanted to do was override the function in each class, and since what I was doing was changing an argument in each case it would not work as the signature must be exactly the same.  By using the base class I can overirde because the signature remains the same.

I hope you enjoy Lucerne when I was there in 2003 I took a tour around the lake and then the funicular railway to the top of Mount Pilatus and then had a cracking lunch at one of the hotels at the top and then caught the cable car back down to Lucerne,  it was summer then and easily one of the most memorable days of my life.  Switzerland is great I can see why they won't let just anyone live there.


cheers

Martin.