• Advertisement

• Popular Now

• 13
• 27
• 9
• 9
• 20
• Advertisement
• Advertisement
• Advertisement

[.net] Inheriting Methods and Boxing

This topic is 3190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay what I want to do is rather complicated to explain. I think code will show things better.
class BasePoint
{
Vector3 Position;
}

class PointA: BasePoint
{
int SomeInt;
}
class PointB: BasePoint
{
float SomeFloat;
}

class PointListA
{
List<PointA> PointsList;

Vector3 GetSomething(){return PointsList[0].Position;}
}
class PointListB
{
List<PointB> PointsList;
}


Essentially from this example. All I want to do is somehow copy the GetSomething Method to the BasePointListB class without copying since it only requires infromation from the base class. Any ideas?

Share this post

Share on other sites
Advertisement
Did you try using a generic extension method?

Share this post

Share on other sites
I'm not quite sure how that would help...

Share this post

Share on other sites
If "GetSomething()" is common between PointListA and PointListB, can't you add a base class that both of those inherit? Then either put PointsList in that base class, allowing you to implement GetSomething() in the base class directly, or use an abstract function.

Share this post

Share on other sites
Ah I meant to add a base class for the PointList classes in the code. I'll update it. But the problem with putting the list of points in the PointListBase is PointA and PointB are different.

I'm not sure what an abstract function is. I'll look it up in a sec.

Update: Okay from what I've seen, wouldn't using an abstract function mean I have to copy the function anyway. I see no other way around this other than casting the list to the base class type.

Share this post

Share on other sites
what's wrong with something like this?
class BasePoint{ Vector3 Position;}class PointA: BasePoint{ int SomeInt;}class PointB: BasePoint{ float SomeFloat;}class PointList<PointType>    where PointType : BasePoint{ List<PointType> PointsList; Vector3 GetSomething(){return PointsList[0].Position;}}

That way, you don't even need to separate classes to handle lists of PointA and PointB.

Although, if you do need extra specialisation, you can still derive from PointList
class PointListA : PointList<PointA>{   // special PointA stuff here}class PointListB : PointList<PointB>{   // special PointB stuff here}

HTH

Share this post

Share on other sites
Wow that works perfectly, Thanks! When generic extention method was said my mind went a completely different way with it.

Share this post

Share on other sites
Quote:
 Original post by AntiGuyWow that works perfectly, Thanks! When generic extention method was said my mind went a completely different way with it.

That's actually not a generic extension method. What SiCrane was suggesting* was something like this
public static class PointListUtils{    public static Vector3 GetSomething(this List<PointType> self)        where PointType : BasePoint    {        return self[0].Position;    }}//used like thisList<PointA> points = new List<PointA>;Vector3 blah = points.GetSomething();

That would also work.

*at least I think that's what he was suggesting. I'm hesitant to put virtual words in his mouth [grin]

Share this post

Share on other sites
Yup, close enough. :)

Share this post

Share on other sites

• Advertisement