如何创建一个列表℃的财产; T> [英] How to create a property for a List<T>

查看:112
本文介绍了如何创建一个列表℃的财产; T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人列表< T> newList; 

公开名单< T> NewList
{
{返回newList;}
集合{newList =值;}
}

我要创建这样的事情,但是这是行不通的。这只是一个例子来证明我的目标,因为它是很常见的创作性质研究字符串和int,甚至吨,但我从来没有见过一个List属性
是它甚至有可能做这样的事情,创造了List类型的属性?



修改



我有了正常的性(字符串属性一个普通的类,整数性能等),但我有这个属性存储用户的选择,所以表现层,我不得不将它们转换成字符串,所以我可能是能够将它们存储在对象。现在是有可能有一个List类型的属性添加到多值,存储在一个更好的,清洁的方式,而不是将信息转换成一个字符串,然后把它分解,再加入吧!再次感谢= D



EDIT2



 私人列表< KeyValuePair<字符串,字符串>> _settings; 

公开名单< KeyValuePair<字符串,字符串>> MySettings
{
{返回_settings; }
集合{_settings =价值; }
}



我用您发布确切的代码,但属性仍不会出现在对象的实例,因此我尝试添加代码get和set(我不知道你为什么离开他们空或做它意味着什么?),并且还增加了在类私有变量,但它仍然没有出现在的属性对象的实例!



我希望你能提供确切的代码来实现此属性和一个简单的代码来分配或检索/这个类对象$ b的一个实例$ b这是第一次,甚至听到这个KeyValuePair和所有的教程都是我的情况很简单,没​​有,对不起



最后编辑:后大量的研发和马克Avenius的帮助下,我找到了完美的答案。希望每个人都能从中受益。



立即!如何创建一个属性列表:




Options类




 公共类选项
{
私人字符串ID;
私人诠释的选择;

公众诠释ID
{
{返回ID; }
集合{ID =价值; }
}

公共字符串选项
{
{返回选项; }
集合{选项=值; }
}
}




的用户类




 公共类用户

{
私人诠释用户名;
私人字符串传递;
私有列表<选项> USEROPTIONS =新的List<选项>();


公众诠释ID
{
{返回用户id; }
组{用户=用户id; }
}

公共字符串传递
{
{返回通; }
集合{传递=价值; }
}

公开名单<选项> OptionsList
{
{返回USEROPTIONS; }
集合{USEROPTIONS =价值; }
}
}




表示层




 用户NEWUSER =新用户(); 
选项userOption =新的选项();

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

Item.Options.Add(userOption);


解决方案

T 必须在您正在使用的范围内确定。

 <$ C:所以,如果你的类是 T 通用您发布什么工作$ C>公共类MyClass的< T> 
{
私有列表< T> newList;

公开名单< T> NewList
{
{返回newList;}
集合{newList =值;}
}
}


否则,您必须使用一个定义类型



编辑:每@ lKashef的要求,下面是如何有一个列表属性:

 私人列表< INT> newList; 

公开名单< INT> NewList
{
{返回newList;}
集合{newList =值;}
}

这可以在非通用类中去。



编辑2:
在回答你的第二个问题(在你的编辑),我不会推荐使用列表这种类型的数据处理(如果我理解正确你)。我会把自己的类的用户设置(或结构,如果你愿意的话),并有该类型的属性在你的原班:

 公共类UserSettings 
{
字符串名字{获得;组; }
字符串名字{获得;组; }
//等
}

公共MyClass类
{
串MyClassProperty1 {搞定;组; }
//等

UserSettings MySettings {搞定;组; }
}



这样的话,你已经命名的属性,你可以代替参考任意的索引列表。例如,您可以参考 MySettings.FirstName ,而不是 MySettingsList [0]

$ B $ ; b

让我知道如果您有任何进一步的问题。



修改3:
。对于这个问题在评论中,你的财产将是这样的:

 公共类MyClass的
{
公开名单< KeyValuePair<字符串,字符串>> MySettings {搞定;组; }
}

修改4:基于问题的编辑2,以下是我会怎么用这样的:

 公共类MyClass的
{
//注意这种类型的财产申报的被称为自动属性和
//这意味着,你写了同样的事情(私人后盾变量用于在幕后,但你看不到它)
酒店的公共列表< KeyValuePair<字符串,字符串> MySettings {搞定;组; }
}

公共类MyConsumingClass
{
公共无效的MyMethod
{
MyClass的MyClass的=新MyClass的();
myClass.MySettings =新的List< KeyValuePair<字符串,字符串>>();
myClass.MySettings.Add(新KeyValuePair<字符串,字符串>(SomeKeyValue,someValue中));

//等
}
}

你提到房地产仍然不会出现在对象的实例,而我不知道你的意思。难道这个属性不会出现在智能感知?你确定你已经创建 MyClass的的一个实例(如 myClass.MySettings 以上),或者是你想访问它像一个静态属性(如 MyClass.MySettings )?


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);

解决方案

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)?

这篇关于如何创建一个列表℃的财产; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆