Sign in to follow this  
AntiGuy

[.net] Inheriting Methods and Boxing

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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites
Quote:
Original post by AntiGuy
Wow 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 this

List<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


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this