首页 > 解决方案 > 如何修复错误 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();
        }
    }

我真的不知道为什么它不会建立。

标签: c#forms

解决方案


您没有为您在 Tabelle 类的第 10 行声明的变量分配任何内容。你可能想让 Artikel 成为一个类,然后试试这个:

Artikel a = new Artikel();
a.id = data[0];

编辑:如果您决定使用结构,这个答案可能会有所帮助:C# Structs: Unassigned local variable?


推荐阅读