Jump to content
  • Advertisement
Sign in to follow this  

C# "templates"

This topic is 4957 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

Hey, I was fooling around with the so called generics:
    public class CSceneGraphOperation<AcceptType>
    {
        public void CollectData<NodeType>(NodeType node)
        {
            if (node is AcceptType)
            {
                AcceptType node = (AcceptType)node;
            }
        }
}

This doesn't work... "cannot convert NodeType to AcceptType" Could someone help me out?

Share this post


Link to post
Share on other sites
Advertisement
Standard rules of inheritance/assignment still apply. NodeType must be a child or be of the same type as AcceptType in order to perform your assignment.


AcceptType node = (AcceptType) node;


Consider


ParentType parent = (ParentType) child;


Right, assuming Child derives from Parent.

Or.


ChildType child = (ParentType) parent;


Wrong. Can't assign a parent type to a child.

If you're attempting to declare a dual parameter template you must use syntax similar to


public class CSceneGraphOperation<AcceptType, NodeType>

Share this post


Link to post
Share on other sites
Oki, thx!

I'm just not 100% comfortable with C#, just moved from c++ :)

So I thought that the cast would work, since it would be a
forced cast... but seems to work different in C#

But it works like this:

AcceptType node = (AcceptType)(Object)node;

Share this post


Link to post
Share on other sites
Is AcceptType a base class of NodeType? In that case you could do something like this:

public class CSceneGraphOperation<AcceptType>
{
public void CollectData<NodeType>(NodeType node) where NodeType : AcceptType
{
AcceptType accept = node; // Casting to base is always well defined.
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!