如何将值添加到另一个类的列表中 [英] How to add the values to the list from another class

查看:75
本文介绍了如何将值添加到另一个类的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Record
   {

        public List<Fie> Field = new List<Fie>();



}




}

public class Fie
   {       
public string Byt { get; set; }
public string Mon(string a)
             {
           string b=a;
           return a;      
 }
  }









我我在函数Mon中得到一个字符串。然后我必须将该值发送到Public string Byt。之后,我必须在列表字段中添加Byt。我不知道该怎么做。请发布答案。在此先感谢。





I am getting a string in function Mon. Then I have to send that value to Public string Byt. After that I have to add the Byt in the list Field. I dont know how to do. Please post the answer. Thanks in Advance.

推荐答案

从这里开始:演练:创建自己的集合类 [ ^ ]


你不能把它从Fie类中添加到列表中 - 它没有' t(并且绝对不应该)知道Fie实例列表的存在,因此无法以任何方式访问它。



添加新的你的列表中的Fie的例子很简单:

You can't add it to the list from within the Fie class - it doesn't (and definitely shouldn't) know even of the existence of the List of Fie instances so it can't access it in any way.

To add a new instance of Fie to your list is simple:
public class Record
   {
   public List<Fie> Field = new List<Fie>();
   void AddIt(string s)
      {
      Field.Add(new Fie(s));
      }
   }



但直接公开公开你的字段也被认为是不好的做法 - 你应该使用一个属性(并且可能返回一个列表的副本而不是实际的列表本身。)


But it is also considered poor practice to publicly expose your fields directly - you should use a property instead (and possibly return a copy of the list instead of the actual list itself).


这篇关于如何将值添加到另一个类的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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