2017-12-01 4 views
2

이 코드를 작성했는데 오류가있는 것 같습니다. 이러한 오류 내가 얻고 있습니다 loopteller++;를 들어컴파일러 오류는 로컬 변수를 할당하지 않았으며 이름은 현재 컨텍스트에 존재하지 않는다고 말합니다.

내가 들어

오류 "할당되지 않은 지역 변수 loopteller의 사용"을 얻을 내 모든 intpos이 오류가

"현재 컨텍스트에 존재하지 않는다"GET

내 코드의 목표는 버튼을 클릭 할 때 파일을 읽고 특정 단어를 내 텍스트 파일에서 가져 오는 양식을 만드는 것입니다. 예, System.IO을 사용하고 있습니다.

loopteller를 들어
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

네 - 당신은'loopteller'를 증가시키고 있지만, 당신은 초기 값을 지정하지 않았습니다. 너는 그렇게 할 수 없다. 'intpos *'오류에 관해서는 실제로 선언하고있는 변수 인'intpos1'이 아니라'inpost1'을 봐주세요. 그래서 그것들은 그냥 오타입니다 (배열이나 컬렉션을 사용하는 것이 더 낫지 만). –

답변

3

++; 나는

사실

와 잘못을 정확하게 무엇 오류 "할당되지 않은 지역 변수 loopteller의 사용"을 얻는다. 처음에는 값을 지정하지 않았으므로 이제 ++을 사용하여 값을 하나씩 계산하려고합니다. 그것이 작동하는 방식이 아닙니다. 당신이하기 전에 값을 지정하면, 당신은 다른 모든 변수와 함께했습니다. 나는이 오류가 사실

"현재 컨텍스트에 존재하지 않는다"얻을 내 모든 intpos를 들어

. 당신이 선언 된 변수는되지 t에서, tX INPOS POS X을 지정됩니다.

요약하면 : 예, 컴파일러가 맞습니다. 들어보고 코드를 수정하십시오.