39
private List<T> newList;

public List<T> NewList
{
get{return newList;}
set{newList = value;}
}

I want to create something like this, but this is won't work. it's just an example to demonstrate my goal as it's pretty common creating proprties for string and int and even T but I've never seen a List property Is it even possible do such a thing, creating a property for type List ?

EDIT

I have a normal class that has normal properties (string properties, int properties, etc) but I have this property that stores user options, So on the presentation layer I had to convert them into a string so I could be able to store them in the Object. Now is it possible to have a property of type List to store the multivalues in a better and clean way, instead of converting information into one string and then split it and again join it! Thanks Again =D

EDIT2

private List<KeyValuePair<string, string>> _settings;

public List<KeyValuePair<string, string>> MySettings
{
    get { return _settings; }
    set { _settings = value; }
}

I used the exact code you posted but the property still won't appear in the object's instance, so I tried adding code in the get and set (I wonder why you left them empty or does it means something?) and also added a private variable in the class but still it doesn't appear in the properties of the object's instance!

I hope you could provide the exact code to implement this property and a simple code that assigns or retrieves from/to an instance of this class object It's the first time to even hear about this KeyValuePair and all the tutorials are pretty simple and not for my case, sorry!

The Last Edit: After a lot of researching and the help of Mark Avenius I found the perfect answer. hope everyone can benefit from this.

NOW! HOW TO CREATE A PROPERTY FOR A LIST :

The Options Class

Public Class Options
{
        private string id;
        private int option;

        public int ID
        {
            get { return id; }
            set { id= value; }
        }

        public string Option
        {
            get { return option; }
            set { option = value; }
        }
}

The Users Class

public class Users

{
        private int userId;
        private string pass;
        private List<Options> userOptions = new List<Options>();


        public int ID
        {
            get { return userId; }
            set { user = userId; }
        }

        public string Pass
        {
            get { return pass; }
            set { pass = value; }
        }

        public List<Options> OptionsList
        {
            get { return userOptions; }
            set { userOptions = value; }
        }
}

The Presentation Layer

    Users newUser = new Users ();
    Options userOption = new Options ();

    userOption.ID = int.Parse(txtBxID.Text);
    userOption.Option = txtBxOption.Text;

    Item.Options.Add(userOption);
Mazen Elkashef
  • 3,430
  • 6
  • 44
  • 72

6 Answers6

45

T must be defined within the scope in which you are working. Therefore, what you have posted will work if your class is generic on T:

public class MyClass<T>
{
    private List<T> newList;

    public List<T> NewList
    {
        get{return newList;}
        set{newList = value;}
    }
}

Otherwise, you have to use a defined type.

EDIT: Per @lKashef's request, following is how to have a List property:

private List<int> newList;

public List<int> NewList
{
    get{return newList;}
    set{newList = value;}
}

This can go within a non-generic class.

Edit 2: In response to your second question (in your edit), I would not recommend using a list for this type of data handling (if I am understanding you correctly). I would put the user settings in their own class (or struct, if you wish) and have a property of this type on your original class:

public class UserSettings
{
 string FirstName { get; set; }
 string LastName { get; set; }
 // etc.
}

public class MyClass
{
 string MyClassProperty1 { get; set; }
 // etc.

 UserSettings MySettings { get; set; }
}

This way, you have named properties that you can reference instead of an arbitrary index in a list. For example, you can reference MySettings.FirstName as opposed to MySettingsList[0].

Let me know if you have any further questions.

EDIT 3: For the question in the comments, your property would be like this:

public class MyClass
{
    public List<KeyValuePair<string, string>> MySettings { get; set; } 
}

EDIT 4: Based on the question's edit 2, following is how I would use this:

public class MyClass
{
    // note that this type of property declaration is called an "Automatic Property" and
    // it means the same thing as you had written (the private backing variable is used behind the scenes, but you don't see it)
    public List<KeyValuePair<string, string> MySettings { get; set; } 
}

public class MyConsumingClass
{
    public void MyMethod
    {
        MyClass myClass = new MyClass();
        myClass.MySettings = new List<KeyValuePair<string, string>>();
        myClass.MySettings.Add(new KeyValuePair<string, string>("SomeKeyValue", "SomeValue"));

        // etc.
    }
}

You mentioned that "the property still won't appear in the object's instance," and I am not sure what you mean. Does this property not appear in IntelliSense? Are you sure that you have created an instance of MyClass (like myClass.MySettings above), or are you trying to access it like a static property (like MyClass.MySettings)?

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
  • @Mark Avenius- and what would be the code if I used a defined type, say int! – Mazen Elkashef Dec 16 '10 at 16:32
  • @Mark Avenius- I did, Thanks Mark =D – Mazen Elkashef Dec 16 '10 at 16:48
  • @Mark Avenius- I've got a small question around the same area -Hope you could help!- Please check my edit and tell me if you can help =) – Mazen Elkashef Dec 17 '10 at 11:23
  • @Mark Avenius- Thanks for following up :D...your solution is fine but it won't fit my situation because I have like a moderator page that has the ability to add new options, and this options are stored into a database (OptionId, Option) and loaded into a CheckBoxList that we could choose from later to assign any of them to the user, so if we added new a new option it won't have a property in our class and if we deleted an option then our class will contain some unnecessary data...This is the whole picture I hope I made it a bit easier for you! – Mazen Elkashef Dec 17 '10 at 15:11
  • 1
    @IKashef: In that case, you could put them in a property of type Dictionary (or if you wish). If you need to have them specifically ordered, you could use a List>. – Mark Avenius Dec 17 '10 at 15:29
  • @Mark Avenius- That's PERFECT +1 I just want to know the how to create a variable of this List> and a property in my class ? but the solution itself is brilliant I think that's exactly what I need – Mazen Elkashef Dec 17 '10 at 15:40
  • @Mark Avenius- I Hope it's the last thing!!! Sorry! I needed an advice regarding your last edit which works perfectly fine, thanks to you...but now I've got a conflict with my application architecture. you see, every pair in the MySettings List is supposed to be an Object of a class Settings which has two values (id and name).I need to have this class in my design as it's an independent entity. and when I first started to ask about the List property is because I want to store those pairs as an object with two properties... – Mazen Elkashef Dec 17 '10 at 22:11
  • Part2 >> so do you think there's a solution ? or I do I just use sth like this > and neglect the second part and just use the Object that will have the (OptionId, Option) ? am sorry, I've been bugging you with all my questions..if you can't answer please let me know I'll understand. =) Thanks for all your effort – Mazen Elkashef Dec 17 '10 at 22:13
  • @Mark Avenius- I don't know if you're still following this question or not but I'm really glad that I finally found the perfect solution and the answer to my question and I'll finally stop bugging you...please check my last edit to see the solution I finally got =) I really appreciated the help, thank you! – Mazen Elkashef Dec 18 '10 at 16:53
4

Simple and effective alternative:

public class ClassName
{
    public List<dynamic> MyProperty { get; set; }
}

or

public class ClassName
{
    public List<object> MyProperty { get; set; }
}

For differences see this post: List<Object> vs List<dynamic>

Community
  • 1
  • 1
Onur Omer
  • 506
  • 3
  • 12
3
public class MyClass<T>
{
  private List<T> list;

  public List<T> MyList { get { return list; } set { list = value; } }
}

Then you can do something like

MyClass<int> instance1 = new MyClass<int>();
List<int> integers = instance1.MyList;

MyClass<Person> instance2 = new MyClass<Person>();
IEnumerable<Person> persons = instance2.MyList;
Rune
  • 8,340
  • 3
  • 34
  • 47
1

You could do this but the T generic parameter needs to be declared at the containing class:

public class Foo<T>
{
    public List<T> NewList { get; set; }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

It's possible to have a property of type List<T> but your class needs to be passed the T too.

public class ClassName<T>
{
  public List<T> MyProperty { get; set; }
}
Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46
0

Either specify the type of T, or if you want to make it generic, you'll need to make the parent class generic.

public class MyClass<T>
{
  etc
devdigital
  • 34,151
  • 9
  • 98
  • 120