简体   繁体   English

编译器错误说我尚未分配局部变量,而在当前上下文中名称不存在?

[英]Compiler error says I have not assigned a local variable & names do not exist in current context while it does?

I wrote this code, it seems to have some errors. 我写了这段代码,似乎有一些错误。 These are the errors I am getting: 这些是我得到的错误:

For loopteller++; 对于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" 对于我所有的intpos我都会收到此错误“当前上下文中不存在”

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 . 是的,我正在使用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
                }
            }
        }

    }
}

For loopteller++; 对于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" 对于我所有的intpos,我都会收到此错误“当前上下文中不存在”

That's true to. 的确如此。 The variables you declared are named inpos t X , not in t pos X . 您声明的变量名为inpos t X ,而不是in t pos X。

So in short: yes, you compiler is right. 简而言之:是的,您的编译器是正确的。 Listen to it and fix your code. 听它并修复您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调试时,当前上下文中不存在变量 - Variable does not exist in the current context while debugging 使用当前上下文中不存在的未分配局部变量 - Use of unassigned local variable that does not exist in current context 变量在当前上下文中不存在 - Variable does not exist in current context 当前上下文中不存在变量? - Variable does not exist in the current context? 变量在当前上下文中不存在 - Variable does not exist in the current context 如何在Jquery中正确调用C#变量。 “变量名称”在当前上下文中不存在 - How do I properly call a C# variable in Jquery. The 'variable name' does not exist in the current context 我遇到此错误“ ContextCompat在当前上下文中不存在”和“ PackageManager不包含PERMISSION_GRANTED定义” - I have this error “ContextCompat does not exist in the current context” and a “PackageManager does not contain PERMISSION_GRANTED definition” '变量已赋值,但从未使用过它的值' & '该名称在当前上下文中不存在' - 'The variable is assigned but its value is never used' & 'The name does not exist in the current context' 错误:在当前上下文中不存在 - error: Does not exist in the current context 错误“当前上下文中不存在” - Error “Does not exist in current context”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM