[英]c# List<t> within a class
This a fairly simple question, I have 3 classes (see code below) called projects, sites and wells. 这是一个相当简单的问题,我有3个类(请参见下面的代码),分别称为项目,站点和井。 The projects class will be a list, but I would like it to contain a list of the sites and then the sites list will contain a list of wells. projects类将是一个列表,但我希望它包含一个站点列表,然后该站点列表将包含一个井列表。 Not sure if that's allowed. 不知道是否允许。 I did a bit of searching and most of the solutions I saw talked more about how to create a list type without knowing what class it was going to be. 我做了一些搜索,看到的大多数解决方案都更多地讨论了如何创建列表类型,而又不知道它将是什么类。 I know what mine are going to be, so that's not an issue here. 我知道我的将是什么,所以这不是问题。
Just to give this a visual
list<t> Projects
list<t> Sites
list<t> Wells
namespace EDMNoCost
{
public partial class Form1 : Form
{
public class wells
{
public string c_wellname { get; set; }
public string well_id { get; set; }
public string site_id { get; set; }
public wells(string c_wellname, string well_id,string site_id)
{
this.c_wellname = c_wellname;
this.well_id = well_id;
this.site_id = site_id;
}
}
public class sites
{
public string c_wellname { get; set; }
public string site_id { get; set; }
public string project_id { get; set; }
public sites(string c_wellname, string site_id, string project_id)
{
this.c_wellname = c_wellname;
this.site_id = site_id;
this.project_id = project_id;
}
}
public class projects
{
public string project_name { get; set; }
public string project_id { get; set; }
public string site_id { get; set; }
public projects(string project_name, string project_id, string site_id)
{
this.project_name = project_name;
this.project_id = project_id;
this.site_id = site_id;
}
}
public List<projects> projInfo = new List<projects>();
public List<sites> siteInfo = new List<sites>();
public List<wells> well_list = new List<wells>();
}
}
It is totally legal, just add them as public properties: 完全合法,只需将它们添加为公共财产即可:
public class projects
{
public List<sites> siteInfo { get; set; }
...
public projects (string project_name, string project_id, string site_id)
{
this.project_name = project_name;
this.project_id = project_id;
this.site_id = site_id;
this.siteInfo = new List<sites>(); // You can add stuff to the List here if you want, or you can just initialize it.
}
}
You can do the same thing for your other classes as well. 您也可以为其他课程做同样的事情。
it is legal to have that kind of structure and your close, you need to add 1 property for the project and site then you need to change each constructor to initialize the property 具有这种结构和关闭结构是合法的,您需要为项目和站点添加1个属性,然后需要更改每个构造函数以初始化该属性
when it is done, you can use your code like this; 完成后,您可以像这样使用代码;
2 projects 2个项目
first one have two sites 第一个有两个站点
second one have 1 site and 1 well 第二个有1个地点和1口井
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<projects> projInfo = new List<projects>();
projInfo.Add(new projects("1", "1", "1"));
projInfo.Add(new projects("2", "2", "2"));
projInfo[0].siteInfo.Add(new sites("10", "10", "10"));
projInfo[0].siteInfo.Add(new sites("20", "20", "20"));
projInfo[1].siteInfo.Add(new sites("30", "30", "30"));
projInfo[1].siteInfo[0].well_list.Add(new wells("100", "100", "100"));
}
}
public class wells
{
public string c_wellname { get; set; }
public string well_id { get; set; }
public string site_id { get; set; }
public wells(string c_wellname, string well_id,string site_id)
{
this.c_wellname = c_wellname;
this.well_id = well_id;
this.site_id = site_id;
}
}
public class sites
{
public string c_wellname { get; set; }
public string site_id { get; set; }
public string project_id { get; set; }
public List<wells> well_list { get; set; } //new property
public sites(string c_wellname, string site_id, string project_id)
{
this.c_wellname = c_wellname;
this.site_id = site_id;
this.project_id = project_id;
well_list = new List<wells>(); //init the property
}
}
public class projects
{
public string project_name { get; set; }
public string project_id { get; set; }
public string site_id { get; set; }
public List<sites> siteInfo { get; set; } //new property
public projects(string project_name, string project_id, string site_id)
{
this.project_name = project_name;
this.project_id = project_id;
this.site_id = site_id;
siteInfo = new List<sites>(); //init the property
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.