2

I tried to insert an object into a generic BindingList. But if I try to add a specific object the compiler says: "Argument type ... is not assignable to parameter type"

private void JoinLeaveItem<T>(BindingList<T> collection)
    {

        if (collection.GetType().GetGenericArguments()[0] == typeof(UserInformation))
        {
            var tmp = new UserInformation();
            collection.Add(tmp);
        }
    }

Please help me

6
  • 1
    Why use a generic collation if you are using a specific type UserInformation? Commented May 4, 2012 at 8:58
  • Because this code is not complete ... I would like to add sometihing like Commented May 4, 2012 at 9:13
  • var tmp2 = new GroupInformation() Commented May 4, 2012 at 9:13
  • OK. How are GroupInformation and UserInformation related to each other? Is there an inheritance hierarchy between them and any other type you want to add to the collection? Commented May 4, 2012 at 9:16
  • No hierarchy and yes I would like to be able to but any Object into this collection... Commented May 4, 2012 at 9:23

2 Answers 2

1

You cannot have objects of two different types that does not have a common anscestor in a strongly typed list. That is: In your case you will need to different collections unless your two (or more) classes have a common base class.

Try creating overloads instead, like this

private void JoinLeaveItem(BindingList<UserInformation> collection)
{
    collection.Add(new UserInformation());
}

private void JoinLeaveItem(BindingList<GroupInformation> collection)
{
    collection.Add(new GroupInformation());
}

Use it like this

JoinLeaveItem(userInformationCollection)
JoinLeaveItem(groupInformationCollection)

Note: I've inlined the tmp variable.

Sign up to request clarification or add additional context in comments.

2 Comments

The idea is that I can but BindingList as UserInformation or GroupInformation as well!
@user1374495 nice. glad to help. :) If you find the answer helpful, please mark it as accepted by clicking the green checkbox.
0

From what you've described in your comments do you want to do something like this....

private void JoinLeaveItem<T>(BindingList<T> collection)  where T: new()
    { 
            var tmp = new T(); 
            collection.Add(tmp); 
    } 

EDIT If you want to add extra Tests to limit to only the items you specify you could add a big test at the beginning

private void JoinLeaveItem<T>(BindingList<T> collection)  where T: new()
    { 
        if (typeof(T) == typeof(UserInformation) || typeof(T) == typeof(GroupInformation) 
            var tmp = new T(); 
            collection.Add(tmp); 
        } 
    } 

Alternatively you can make a more generic solution via use of an interface.

Define an interface

public interface ILeaveItem { }

Make UserInformation and GroupInformation inherit from it and then use

private void JoinLeaveItem<T>(BindingList<T> collection)  where T: ILeaveItem, new()
    { 
            var tmp = new T(); 
            collection.Add(tmp); 
    } 

3 Comments

Yes but how I can put now specific Objects like UserInformation and or GroupInformations ?
@user1374495 What do you mean? with this code if BindingList is a collection of UserInformation it will determine the type of T and insert a UserInformation object, if BindingList is a collection of GroupInformation it will insert a GroupInformation object
@user1374495, from your comments on the question you've said that you want either GroupInformation or UserInformation to appear in the same collection at the same time?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.