在列表框中插入列表 [英] Insert list in listbox

查看:140
本文介绍了在列表框中插入列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,正在尝试使用C#进行探索,但是我尝试在列表框中添加一个列表.

I'm new to C# and I'm trying to explore in C#, however I try to add a list in a listbox.

我遇到的错误是:Object reference not set to an instance of an object. 知道如何解决这个问题吗?

The error that I'm having is : Object reference not set to an instance of an object. Any idea how to resolve this?

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        something a = something iets();

        public Form1()
        {
            InitializeComponent();
        }
// part1
        class something {

            public List<string> testing { get  ; set; }
        }

// part2
        private void button1_Click(object sender, EventArgs e)
        {
            a.testing.Add("programming");
            a.testing.Add("over");
            a.testing.Add("something");



            foreach (string i in a.testing)
            { listBox1.Items.Add(i); }
        }
    }
}

推荐答案

您必须在访问testing之前对其进行初始化.

You have to initialize testing at some point before accessing it.

也许您可以向something类添加一个构造函数.

Maybe you could add a constructor to the something class.

public something()
{
  testing = new List<string>();
}

并按照上面的评论所述,替换

and as pointed in the comments above, replace

something a = something iets();

下面有这个.

something a = new something(); //this should be the correct thing.

这篇关于在列表框中插入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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