c# - 如何修复错误 CS0165:“使用未分配的局部变量 'a'”?
问题描述
我想从 Windows 窗体中的 CSV 创建输出
public struct Artikel
{
public String id;
public double d;
public double dmin;
public double I;
public double d2;
public double e;
public double I1;
public double I2;
}
现在我宣布路径
private void buttonEingabe_Click(object sender, EventArgs e)
{
var tabelle = Tabelle.getTabel(@"C:\Users\alexa\source\repos\metallBohrrer\metallBohrrer\db.csv");
int index = 0;
int anzahl = tabelle.Length;
string temp = textBoxpk.Text;
int tempint = 0;
for (; index < anzahl;)
{
if (temp == tabelle[index].id)
{
tempint = index;
}
index++;
}
textBoxpk.Text = tabelle[tempint].id;
// textBox2.Text = tabelle[tempint].d;
// textBox3.Text = tabelle[tempint].dmin;
}
之后我想声明它,在这里我看到错误“CS0165 C# Use of unassigned local variable 'a'”和“CS0136 C# A local or parameter named cannot be declared in this scope because the name is used in an enclosure local scope定义本地或参数。”
public static class Tabelle
{
public static Artikel[] getTabel(String Datei)
{
List<Artikel> artikel = new List<Artikel>();
String[] zeilen = File.ReadAllLines(Datei);
foreach (String zeilen in zeilen)
{
String[] data = zeilen.Split(';');
Artikel a;
a.id = data[0];
artikel.Add(a);
}
return artikel.ToArray();
}
}
我真的不知道为什么它不会建立。
解决方案
您没有为您在 Tabelle 类的第 10 行声明的变量分配任何内容。你可能想让 Artikel 成为一个类,然后试试这个:
Artikel a = new Artikel();
a.id = data[0];
编辑:如果您决定使用结构,这个答案可能会有所帮助:C# Structs: Unassigned local variable?
推荐阅读
- python - 根据距离和出现频率选择一个项目(从一组项目中)
- r - 如何在子列表上使用“lapply”(r)
- python-3.x - Rasa Core - 无法访问操作模板名称,因为该名称不是该域的注册操作
- python - 如何在 DRF 中组合/混合对象级别和用户级别权限?
- c - C - 错误:“struct name_s”类型的定义不完整
- docker - 如何使用 ansible 删除过时的容器?
- vb.net - 为什么即使我的程序以管理员身份执行,也无法访问特定文件夹?
- python - 在两列上使用 groupby 后获取第三列的最大值
- reactjs - 为什么我的“音频按钮”不播放声音(onClick)
- c++ - 为什么 PVS-Studio 声明存在非常量参考?