小编Cal*_*ter的帖子

无法添加到类对象中的列表

首先,如果这是一个愚蠢的问题,我会道歉。自 VB6 以来我没有编程任何东西后,我已经使用 C# 16 个小时了。我只是想拼凑一个供个人使用的小程序,它从旧的访问数据库中读取数据并在 Excel 中输出格式化的报告。我为凌乱/低效的代码道歉。

概述:我有两种类类型,“区域”和“设备”。每个“区域”中都有一个设备列表。主程序有一个区域列表。每个数据库中都有不同数量的“区域”,每个“区域”分配给它的设备数量也不同。我需要按顺序解析区域列表和每个区域上的设备。我从结构和数组开始,流行的观点似乎是这两种方法都不好,反正我运气不好,所以我转向了列表和类,而且进展顺利。

我可以从数据库中提取所有“区域”,将它们添加到列表中,为它们分配标签和 ID。问题是当我从数据库中读取“设备”时,我无法将它们添加到区域内的列表中。

这是我得到的错误:“未将对象引用设置为对象的实例。” 我收集的意思是对象为空?

这是相关的代码:

设备类别:

    public class Device
    {
        public string Label;
        public string Address;
        public string Type;
        public Device(string Label, string Address, string Type)
        {
            this.Address = Address;
            this.Label = Label;
            this.Type = Type;
        }
    }
Run Code Online (Sandbox Code Playgroud)

区域类:

    public class Zone
    {
        public string Label;
        public short ID;
        public List<Device> Devices;

        public Zone(string Label, short ID) {
            this.Label = Label;
            this.ID = ID;
            // ADDED AS PER SUGGESTIONS BELOW
            this.Devices …
Run Code Online (Sandbox Code Playgroud)

c# class list population

1
推荐指数
1
解决办法
7534
查看次数

标签 统计

c# ×1

class ×1

list ×1

population ×1