Advertisement Jump to content
Sign in to follow this  
IndyOfComo

List<MyParentClass>

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

Somewhere in stackoverflow I found a question similar to my situation, which was like finding a needle in a haystack. But I foolishly closed my browser before I was completely done with my coding.

 

Here's my goal....

 

class BasicComponent

{

     List<GroupIcon> icon;

}

 

class ItemGroup : BasicComponent

{

     icon.Add(new ItemIcon(seed));

}

 

class MonsterGroup : BasicComponent

{

     icon.Add(new MonsterIcon(seed));

}

 

class GroupIcon {}

class ItemIcon : GroupIcon {}

class MonsterIcon : GroupIcon {}

 

Using an interface for the icons worked great, until I needed additional methods for certain icons--not just different execution of the methods, but whole new methods. Since of course if the method wasn't defined in the interface the compiler couldn't find it, and I didn't want to add it to the interface and have to define it in all the other classes which didn't need it.

 

The answer I had found at stackoverflow was basically making my own List<> which only accepted certain types of objects. The code looked something like this:

class IconList<T> : List<Type>
        {
            public void Add(Type t)
            {
                if (!typeof(GroupIcon).IsAssignableFrom(t))
                {
                    throw new ArgumentException("Type must be IGroupIcon, or implement/inherit it.");
                }
                else
                {
                    base.Add(t);
                }
            }
        }

But this isn't really working for me yet, so I have something wrong. For instance, this accessor...

public GroupIcon SelectedIcon
        {
            get { return icons[selectedPointer]; }
        }

can't compile cause it's trying to convert Type into GroupIcon. And if I change it to Add(GroupIcon t) then IsAssignableFrom() isn't valid. (of course).

 

What'd I do wrong here?

Edited by IndyOfComo

Share this post


Link to post
Share on other sites
Advertisement

public GroupIcon SelectedIcon
        {
            get { return icons[selectedPointer]; }
        }

can't compile cause it's trying to convert Type into GroupIcon. And if I change it to Add(GroupIcon t) then IsAssignableFrom() isn't valid. (of course).

 

What'd I do wrong here?

 

I don't understand your code. Although, it looks like your List is of type Icon, but your item icons are subclasses of GroupIcon

The List should be <GroupIcon> ?

 

Also, get should return:

get { return icons[selectedPointer] as GroupIcon; }

 

meaning, you are being returned a GroupIcon, or one of its derivatives

 

I think all of your problems will go away, simply by making the lists not <T> but <GroupIcon>

and call it a day :P

Edited by Kaptein

Share this post


Link to post
Share on other sites

 


public GroupIcon SelectedIcon
        {
            get { return icons[selectedPointer]; }
        }

can't compile cause it's trying to convert Type into GroupIcon. And if I change it to Add(GroupIcon t) then IsAssignableFrom() isn't valid. (of course).

 

What'd I do wrong here?

 

I don't understand your code. Although, it looks like your List is of type Icon, but your item icons are subclasses of GroupIcon

The List should be <GroupIcon> ?

 

Also, get should return:

get { return icons[selectedPointer] as GroupIcon; }

 

meaning, you are being returned a GroupIcon, or one of its derivatives

 

I think all of your problems will go away, simply by making the lists not <T> but <GroupIcon>

and call it a day tongue.png

 

 

Sorry, slight mistake in the goal code...fixed BasicComponent.

 

Before going down this path, I had tried just having List<GroupIcon> in BasicComponent but then ItemGroup with List<ItemIcon> didn't have access to the methods particular to ItemIcon.

(There isn't an inverse of base., is there? Where I could have the parent say 'go execute the child's version'?)

Share this post


Link to post
Share on other sites

No, there isn't.. but there is no reason why you can't have a BaseIcon class, with all the abstract functions you actually need,

such as render() and so on

 

If all the icons have things in common, you could also just implement most functions in this BaseIcon class

Don't be afraid to increase the height of the hierarchy. I remember doing it, because I had no choice. It worked out fine.

 

Checking if an object is of a particular type:

if (x is GroupIcon)

{

    (x as GroupIcon).doGroupIconStuff();

}

Share this post


Link to post
Share on other sites

Would this work?

 

class BasicComponent<T extends GroupIcon>

{

     List<T> icon;

}

 

class ItemGroup : BasicComponent<ItemIcon>

{

     icon.Add(new ItemIcon(seed));

}

 

class MonsterGroup : BasicComponent<MonsterIcon>

{

     icon.Add(new MonsterIcon(seed));

}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!