I wrote this code, it seems to have some errors. These are the errors I am getting:

For loopteller++; I get a error "Use of unassigned local variable loopteller"

For all my intpos I get this error "Does not exist in the current context"

The goal of my code is to make a form that reads files and gets specific words out of my text file when I click on a button. Yes I am using System.IO.

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string interface1 = "";
        string interface2 = "";
        string interface3 = "";
        string interface4 = "";
        int inpost1 = 0;
        int inpost2 = 0;
        int inpost3 = 0;
        int inpost4 = 0;
        int teller = 0;
        int interfaceteller = 0;
        int loopteller;


        string[] routerconfig = File.ReadAllLines("c:\\naamcomputer\\pt.txt");
        foreach(string configregel in routerconfig)
        {
            loopteller++;

            if (configregel.Contains("interface Gigabitethernet"))
            {
                teller++;
                if(teller == 1)
                {
                    interface1 = configregel;
                    intpos1 = loopteller;
                }
                else if(teller == 2)
                {
                    interface2 = configregel;
                    intpos2 = loopteller;
                }
                else if (teller == 3)
                {
                    interface3 = configregel;
                    intpos3 = loopteller;
                }
                else if (teller == 4)
                {
                    interface4 = configregel;
                    intpos4 = loopteller
                }
            }
        }

    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

For loopteller++; I get a error "Use of unassigned local variable loopteller"

That's true and exactly what's wrong. You never assigned a value in the first place and now you want to count it one up by using ++. That's not how it works. Assign it a value before you do, you did it with all other variables.

For all my intpos I get this error "Does not exist in the current context"

That's true to. The variables you declared are named inpost X, not intpos X.

So in short: yes, you compiler is right. Listen to it and fix your code.