Skip to main content
GameDev.net gamedev.net
🔒 Locked

List<MyParentClass>

Started by IndyOfComo Dec 4, 2013 at 9:47 PM 3 replies 1.6k views
Original Post
IndyOfComo
IndyOfComo

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

Here is my technical background info.
Kaptein
Kaptein


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 ?

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 but

and call it a day :P

IndyOfComo
IndyOfComo


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 ?

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 but

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 in BasicComponent but then ItemGroup with List 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'?)

Here is my technical background info.
Kaptein
Kaptein

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();

}

de_mattT
de_mattT

Would this work?

class BasicComponent

{

List icon;

}

class ItemGroup : BasicComponent

{

icon.Add(new ItemIcon(seed));

}

class MonsterGroup : BasicComponent

{

icon.Add(new MonsterIcon(seed));

}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.