Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I want to add a member to a collection of a generic class, but it generates two errors as below:
Error   1   The best overloaded method match for 'Test.TransitionToolManager.Add(Test.StyleHandleCollection<object>)' has some invalid arguments

Error   2   Argument 1: cannot convert from 'Test.StyleHandleCollection<Test.Plane>' to 'Test.StyleHandleCollection<object>'

What am I doing wrong?
Here is the code:
namespace Test
{
    public interface ITransitionTool
    {
    }

    public class Plane : ITransitionTool
    {
    }

    public class Car : ITransitionTool
    {
    }

    public class Style
    {
        private Color mColor = new Color();
    }

    public class StyleHandle<T>
    {
        private List<T> mUserObjects = new List<T>();

        public void Adduser(T obj)
        {
            mUserObjects.Add(obj);
        }

        public void RemoveUser(T obj)
        {
            mUserObjects.Remove(obj);
        }
    }

    public class StyleHandleCollection<T>
    {
        public Dictionary<string, StyleHandle<T>> mDict = null;

        public StyleHandleCollection()
        {
            mDict = new Dictionary<string, StyleHandle<T>>();
        }
    }

    public class TransitionToolManager
    {
        private Dictionary<Type, StyleHandleCollection<object>> mCollection = null;

        public TransitionToolManager()
        {
            mCollection = new Dictionary<Type, StyleHandleCollection<object>>();
        }

        public void Add(StyleHandleCollection<object> collection)
        {
            mCollection.Add(typeof(object), collection);
        }
    }

    public class Document
    {
        private TransitionToolManager mMgr = null;

        public Document()
        {
            mMgr = new TransitionToolManager();
            StyleHandleCollection<Plane> planeStyles = new StyleHandleCollection<Plane>(); // error happens in this line...
            mMgr.Add(planeStyles);
        }
    }
}
Posted
Updated 24-Apr-11 15:18pm
v12
Comments
R. Giskard Reventlov 24-Apr-11 4:47am    
Well, sorry about that: can't seem to get it to format correctly. I'll remove the tags: perhaps someone else could try?
Ed Nutting 24-Apr-11 5:50am    
Done. You needed top replace all the '<' and '>' signs with '<' and '>' then put <pre> blocks round the code and uncheck the 'Ignore HTML in this post' option.
R. Giskard Reventlov 24-Apr-11 11:36am    
Thanks: not sure what came over me! :-)
DaveLand 24-Apr-11 21:12pm    
Thanks for your correction.

Your issue seems to be that you pass Test.TransitionToolManager.Add a StyleHandleCollection of type Test.Plane when the method requires it to be of type object. You are trying to pass in the wrong collection type. Make your planeStyles of type StyleHandleCollection<object> and see if you still get the error.

Hope this helps,

Ed :)
 
Share this answer
 
v2
Comments
DaveLand 24-Apr-11 20:54pm    
Thanks for your inputs, however, here I want to get a collection with different types.

Such as "mMgr = new TransitionToolManager();

StyleHandleCollection<plane> planeStyles = new StyleHandleCollection<plane>();

mMgr.Add(planeStyles); // Compile error happens in this line...

StyleHandleCollection<car> carStyles = new StyleHandleCollection<car>();

mMgr.Add(carStyles); ..."

And I don't want TransitionToolManager is a generic class too.
Ed Nutting 25-Apr-11 6:15am    
Sorry I should have realised sooner, all you should have to do is cast the planeStyles to the correct type like so:

mMgr.Add((StyleHandleCollection<object>)planeStyles);

That should solve your problem. Ed :)
DaveLand 26-Apr-11 4:24am    
Hi Ed,

Thanks for your response. I tested it, however it doesn't work well. The error changes as,
Error 1 Cannot convert type 'Test.StyleHandleCollection<test.plane>' to 'Test.StyleHandleCollection<object>'.

So it seems that it needs a basic class or interface for StyleHandleCollection<t> that can be used as a general value in the dictionary.
After I change StyleHandleCollection<t> derived from IStyleCollection and use it in the dictionary, it works.
Here is my solution, though it is not perfect, it works. :)
namespace Test
{
    public interface ITransitionTool
    {
    }

    public class Plane : ITransitionTool
    {
    }

    public class Car : ITransitionTool
    {
    }

    public interface IStyleCollection
    {
    }

    public class Style
    {
        private Color mColor = new Color();
    }

    public class StyleHandle<T>
    {
        private List<T> mUserObjects = new List<T>();

        public void Adduser(T obj)
        {
            mUserObjects.Add(obj);
        }

        public void RemoveUser(T obj)
        {
            mUserObjects.Remove(obj);
        }
    }

    public class StyleHandleCollection<T> : IStyleCollection
    {
        public Dictionary<string, StyleHandle<T>> mDict = null;

        public StyleHandleCollection()
        {
            mDict = new Dictionary<string, StyleHandle<T>>();
        }
    }

    public class TransitionToolManager
    {
        private Dictionary<Type, IStyleCollection> mCollection = null;

        public TransitionToolManager()
        {
            mCollection = new Dictionary<Type, IStyleCollection>();
        }

        public void Add<T>(StyleHandleCollection<T> collection)
        {
            mCollection.Add(typeof(object), collection);
        }
    }

    public class Document
    {
        private TransitionToolManager mMgr = null;

        public Document()
        {
            mMgr = new TransitionToolManager();
            StyleHandleCollection<Plane> planeStyles = new StyleHandleCollection<Plane>();
            mMgr.Add(planeStyles);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900