Public Group

# List<MyParentClass>

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

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

{

}

class MonsterGroup : BasicComponent

{

}

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>
{
{
if (!typeof(GroupIcon).IsAssignableFrom(t))
{
throw new ArgumentException("Type must be IGroupIcon, or implement/inherit it.");
}
else
{
}
}
}



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 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 :P

Edited by Kaptein

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

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 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 on other sites

Would this work?

class BasicComponent<T extends GroupIcon>

{

List<T> icon;

}

class ItemGroup : BasicComponent<ItemIcon>

{

}

class MonsterGroup : BasicComponent<MonsterIcon>

{

}

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11