2014-01-07 3 views
-3

나는이 코드를 컴파일 할 때 나는 "아마 오해 빈 문"경고를 받고 있어요 :아마도 잘못된 빈 문 경고

class Lab6 
{ 
    static void Main(string[] args) 
    { 
     Program fileOperation = new Program(); 

     Console.WriteLine("Enter a name for the file:"); 
     string fileName = Console.ReadLine(); 

     if (File.Exists(fileName)) 
     { 
      Console.WriteLine("The file name exists. Do you want to continue appendng ? (Y/N)"); 
      string persmission = Console.ReadLine(); 

      if (persmission.Equals("Y") || persmission.Equals("y")) 
      { 
       fileOperation.appendFile(fileName); 
      } 
     } 
     else 
     { 

      using (StreamWriter sw = new StreamWriter(fileName)) ; 
      fileOperation.appendFile(fileName); 
     } 
    } 

    public void appendFile(String fileName) 
    { 
     Console.WriteLine("Please enter new content for the file - type Done and press enter to finish editing:"); 
     string newContent = Console.ReadLine(); 
     while (newContent != "Done") 
     { 
      File.AppendAllText(fileName, (newContent + Environment.NewLine)); 
      newContent = Console.ReadLine(); 
     } 
    } 
} 

나는 그것을 해결하기 위해 노력을하지만 난 할 수 없습니다. 이 경고는 무엇을 의미하며 어디에 문제가 있습니까?

+2

질문 할 때 다음 번에 열심히 노력하십시오. 내 편집을보고, 적어도 조금 더 읽고 이해할 수 있도록 노력했습니다. 또한, 제목 "안녕하세요, 저는 새로운 ..."은 ** 실제로 ** 부적절합니다. 제목 **은 ** 귀하의 문제에 대한 간략한 요약이어야합니다. –

+0

의견을 보내 주셔서 감사합니다. – user3164058

+0

"문제가 어디에 있습니까?" - 전체를 제공하지 않은 오류 메시지에는 오류의 행 번호가 들어 있습니다. 'sw'를 설정하는'using' 문은 가지고 있지만'sw'는 사용하지 않습니다. –

답변

9

"아마도 빈 문장이 잘못 들었을 것"이라는 경고는 코드에 문장이 있다는 것을 의미합니다. 즉 화합물 (예 : statement { ... more statement ... })을 포함해야하지만 본문 대신 문장을 종료하는 세미콜론 ; . 어디서 오류가 발생했는지 즉시 알 수 있어야합니다. 경고를 두 번 클릭하면 해당 코드 행을 탐색 할 수 있습니다. 이 성명에서 코드에서,

if (some condition) ; // mistakenly terminated 
    do_something(); // this is always executed 

if (some condition); // mistakenly terminated 
{ 
    // this is always executed 
    ... statement supposed to be the 'then' part, but in fact not ... 
} 

using (mySuperLock.AcquiredWriterLock()); // mistakenly terminated 
{ 
    ... no, no, no, this not going to be executed under a lock ... 
} 

특히 :이 같은

일반적인 실수는 다음과 같다

using (StreamWriter sw = new StreamWriter(fileName)) ; 

하고, 마지막에 ;이 년대 using 빈 (= 쓸모없는). 코드의 바로 다음 줄 :

fileOperation.appendFile(fileName); 

는 어떠한 StreamWriter 함께 할 수 없다, 그래서 당신의 코드에서 누락 분명히 뭔가있다 (또는을 통해 왼쪽 - 뭔가 using을, 아마?).

+2

+1. VS에서 오류가 선택되면 "F1"을 클릭하면 [CS0642] (http://msdn.microsoft.com/en-us/library/9x19t380%28v=90.aspx)에 대한 MSDN 정보를 쉽게 얻을 수 있습니다. –