【发布时间】:2017-08-16 23:40:50
【问题描述】:
我正在尝试将列表框中的项目复制到数组中。 但我在这里遗漏了一些东西。我收到错误方法名称预期。
我有三个错误:
错误 CS1955 不可调用的成员“ListBox.Items”不能像方法一样使用。
错误 CS0201 只有赋值、调用、递增、递减和新对象表达式可以作为语句使用
错误 CS0149 方法名称应为 WindowsFormsApp6
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i;
private void Form1_Load(object sender, EventArgs e)
{
Random rand = new Random();
for (i = 0; i <= 10; i++)
{
listBox1.Items.Add(rand.Next(0, 10));
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
for (i = 0; i <= listBox1.Items.Count - 1; i++)
{
string[] strArray = new string[11];
strArray(i) == listBox1.Items(i);
label1.Text = "Coppied items";
}
}
}
【问题讨论】:
-
使用方括号
[ ]而不是括号( )来引用数组的索引 -
数组使用 [] 访问,而不是 ()。这不是 VB.Net
-
您还应该在本地声明
i,在您的for条件中:for (int i = 0; ... -
您还应该在
for循环之外声明和实例化您的strArray。目前,您在每次迭代时将其设置为一个新数组 -
而
==是一个比较,而不是一个赋值。strArray[i] == listBox1.Items[i];不正确。