c# trying to create generic name value class to use in comboboxes

Hi, I have needs to bind different things to comboboxes.  The value could be an int, string, guid, who knows.  I wanted to create a generic DisplayMemberValueMember class so I can build up a list and bind to it.  For example, I'd want to do something like below.

I can't get my class to compile of the code to create the list to work.  I am a little new to creating my own classes with generics.

My goal (with any object not just bool):
List<DisplayMemberValueMember<bool>> _displaymemberlist;
_displaymemberlist.add("True", true);
_displaymemberlist.add("False", false);


(code I tried)
var displaymemberlist = new List<DisplayMemberValueMember<int>>();
displaymemberlist.Add(new DisplayMemberValueMember<int>("Client1", 1));
displaymemberlist.Add(new DisplayMemberValueMember<int>("Client2", 1));

I can't figure out how to create the list outside this class.


my class

    public class DisplayMemberValueMember<T>
    {
        public string DisplayMember;
        public T ValueMember;

        public DisplayMemberValueMember<T>(string displayMember, object valueMember)
        {
            DisplayMember = displayMember;
            ValueMember = valueMember;
        }
    }
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
This works for me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DisplayMemberDemo
{
    class Program
    {
        static void Main( string[ ] args )
        {
            DisplayMemberValueMember<int> dmvInt = new DisplayMemberValueMember<int>("Hello", 10);

            List<DisplayMemberValueMember<Guid>> dmvGuids = new List<DisplayMemberValueMember<Guid>>();
            dmvGuids.Add( new DisplayMemberValueMember<Guid>("MyGuid", Guid.NewGuid()));
            dmvGuids.Add( new DisplayMemberValueMember<Guid>( "AnotherGuid", Guid.NewGuid() ) );

        }
    }
}

    public class DisplayMemberValueMember<T>
    {
        public string DisplayMember;
        public T ValueMember;

        public DisplayMemberValueMember(string displayMember, T valueMember)
        {
            DisplayMember = displayMember;
            ValueMember = valueMember;
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.