이 코드를 작성했는데 오류가있는 것 같습니다. 이러한 오류 내가 얻고 있습니다 loopteller++;
를 들어컴파일러 오류는 로컬 변수를 할당하지 않았으며 이름은 현재 컨텍스트에 존재하지 않는다고 말합니다.
내가 들어
오류 "할당되지 않은 지역 변수 loopteller의 사용"을 얻을 내 모든 intpos
이 오류가
내 코드의 목표는 버튼을 클릭 할 때 파일을 읽고 특정 단어를 내 텍스트 파일에서 가져 오는 양식을 만드는 것입니다. 예, 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
}
}
}
}
}
네 - 당신은'loopteller'를 증가시키고 있지만, 당신은 초기 값을 지정하지 않았습니다. 너는 그렇게 할 수 없다. 'intpos *'오류에 관해서는 실제로 선언하고있는 변수 인'intpos1'이 아니라'inpost1'을 봐주세요. 그래서 그것들은 그냥 오타입니다 (배열이나 컬렉션을 사용하는 것이 더 낫지 만). –