2 Replies - 396 Views - Last Post: 25 October 2009 - 12:41 PM Rate Topic: -----

#1 Snowblind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 09

Separating Classes in Containers

Post icon  Posted 25 October 2009 - 11:49 AM

Hey, DIC :)
I've recently started trying to learn C#, reading through C# 3.0: A Beginner's Guide (Herbert Schildt). I've almost finished it (Just chapters on LINQ and Preprocessor/RTII/Nullable types left to read). So, in trying to understand things some more, I've been playing around with making some programs, but I'm having a problem with generics.

I have 2 classes that inherit from a base class, and 2 classes that inherit from this base class. What I'm trying to do, is have a container for the base class, and have objects from the other 2 classes in the container. Then have a separate container for the other 2 classes and split the objects from the base container into the other containers.

Something along the lines of this.
class Base { public Base() { } }
class ClassOne : Base { public ClassOne() : base() { } }
class ClassTwo : Base { public ClassTwo() : base() { } }

class Program
{
    static void Exchange<T, V>(List<T> baseList, List<V> listTwo) where T : Base
                                                                  where V : Base
    {
        V objTwo;
        foreach (T obj in baseList)
        {
            if (objTwo.GetType() == obj.GetType())
            {
                listTwo.Add(obj);
                baseList.Remove(obj);
            }
        }            
    }
    static void Main(string[] args)
    {
        List<Base> baseList = new List<Base>();
        List<ClassOne> oneList = new List<ClassOne>();
        List<ClassTwo> twoList = new List<ClassTwo>();
        ClassOne oneObj = new ClassOne();
        ClassTwo twoObj = new ClassTwo();

        baseList.Add(oneObj);
        baseList.Add(twoObj);

        Exchange(baseList, oneList);
    }
}



Obviously, this doesn't work, because you can't convert from T to V. Anyone able to suggest a way of doing this?

I'm could more than likely to this without using generics, but I'd like to try and get this working.

Is This A Good Question/Topic? 0
  • +

Replies To: Separating Classes in Containers

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Separating Classes in Containers

Posted 25 October 2009 - 12:23 PM

You could change your foreach loop to be of type Base rather than T. Also, you can't remove elements from a list in a foreach loop that is iterating through the list.

You can also simplify the if with
if (obj is V) {
Was This Post Helpful? 1
  • +
  • -

#3 Snowblind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-February 09

Re: Separating Classes in Containers

Posted 25 October 2009 - 12:41 PM

Changing the foreach loop to
foreach (Base obj in baseList)
{
	if (obj is V)
	{
		listTwo.Add((V)obj);
	}
}


still doesn't work. Error is: Cannot convert from Base to V.

Edit: Casting to V works though. Thanks :)

This post has been edited by Snowblind: 25 October 2009 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1