Jump to content

  • Log In with Google      Sign In   
  • Create Account


List<MyParentClass>


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 IndyOfComo   Members   -  Reputation: 514

Like
0Likes
Like

Posted 04 December 2013 - 03:47 PM

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, 04 December 2013 - 04:15 PM.

Here is my technical background info.

Sponsor:

#2 Kaptein   Prime Members   -  Reputation: 1842

Like
0Likes
Like

Posted 04 December 2013 - 03:55 PM


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, 04 December 2013 - 03:56 PM.


#3 IndyOfComo   Members   -  Reputation: 514

Like
0Likes
Like

Posted 04 December 2013 - 04:18 PM

 


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


Here is my technical background info.

#4 Kaptein   Prime Members   -  Reputation: 1842

Like
0Likes
Like

Posted 04 December 2013 - 04:52 PM

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

}



#5 de_mattT   Members   -  Reputation: 308

Like
0Likes
Like

Posted 05 December 2013 - 04:58 PM

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

}






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS