创建一个自定义类数组,但赋值时候出错了。出错:未将对象引用设置到对象的实例,请问是什么原因,怎么改?

namespace WindowsFormsApplication1
{
class ff
{
public string name;
public string url;
public string user;
public string pwd;
public Boolean piccode;
public string sf;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ff[] oa = new ff[10];
for (int i = 0; i <= oa.Length - 1; i++)
{
oa[i].name = "姓名变量";//出错:未将对象引用设置到对象的实例。
oa[i].url = "链接变量";
oa[i].user = "用户名变量";
oa[i].pwd = "密码变量";
oa[i].piccode = true;
oa[i].sf = "身份变量";
}
for (int j = 0;j <= oa.Length - 1;j++)
{
textBox2.Text = textBox2.Text + oa[j].name + "\r\n";
textBox2.Text = textBox2.Text + oa[j].url + "\r\n";
textBox2.Text = textBox2.Text + oa[j].user + "\r\n";
textBox2.Text = textBox2.Text + oa[j].pwd + "\r\n";
textBox2.Text = textBox2.Text + oa[j].piccode.ToString() + "\r\n";
textBox2.Text = textBox2.Text + oa[j].sf + "\r\n";
textBox2.Text = textBox2.Text + "-----------------" + "\r\n";
}
}
}
}

第1个回答  2012-04-28
oa[i].name = "姓名变量";//出错:未将对象引用设置到对象的实例。
这句话之前应该
oa[i].= new ff();

这个与C语言不一样,C只需要分配好内存,就可以直接操作了,但C#这些面向对象的语言不一样,你必须初始化ff对象,也就是上面的代码。
第2个回答  2012-04-28
For中要生成oa[I]=new ff()再负值。本回答被提问者采纳
第3个回答  2012-04-28
楼上正解
相似回答