2016-09-08 4 views
-5

リンク参照:底部の自動車ウィンドウがtoParse = "" ことを示すステートメントが実行されている場合、これはなぜ偽ですか? (C#)と

http://i.imgur.com/gFcamd8.png

お知らせ。しかしtoParse!= ""はとにかくtrueと評価され、アプリケーションをクラッシュさせます。このラインnewNode.parseString(inside)は、それがコールスタックでありますことを意味強調表示され、クラッシュ前に呼び出された、画像で

public void parseString(string toParse) 
    { 
     while (toParse != "") 
     { 
      string nextLine = readLine(ref toParse); 

      if (nextLine.IndexOf("//") == 0) 
      { 
       comments.Add(nextLine); 
       continue; 
      } 

      if (nextLine.IndexOf(".") == 0) 
      { 
       string[] declarationParts = nextLine.Split(' '); 
       string declarationString = declarationParts[0].Substring(1, declarationParts[0].Length - 1); 
       declarationString = char.ToUpper(declarationString[0]) + declarationString.Substring(1); 
       DirectiveEnum type = (DirectiveEnum)Enum.Parse(typeof(DirectiveEnum), declarationString); 
       string[] attributes = declarationParts.Skip(1).ToArray(); 
       MSILNode newNode = new MSILNode(type); 
       newNode.addAttributes(attributes); 
       toParse = toParse.Trim(); 
       if (toParse != "") 
       { 
        while (toParse[0] != '{' && toParse[0] != '.' && toParse.IndexOf("//") != 0) 
        { 
         nextLine = readLine(ref toParse); 
         attributes = nextLine.Split(' '); 
         newNode.addAttributes(attributes); 
        } 

        if (toParse[0] == '{') 
        { 
         readLine(ref toParse); 
         string inside = separate(ref toParse, "}"); 

         newNode.parseString(inside); 
        } 
       } 
       subNodes.Add(newNode); 
       continue; 
      } 

      Console.WriteLine(nextLine); 
     } 
    } 
+0

toParseの値は何ですか? –

+0

'toParse'の長さは何ですか? – siride

+0

参照op:toParse = "" –

答えて

0

はここで完全な方法です。この行はおそらくtoParseに ""なるように変異しています。

+0

方法全体を表示するようにOPを修正しました。 parseString()を呼び出すときに最初にチェックするのは、渡された文字列が== ""かどうかです。 –

2

この1つのスナップショットだけが与えられた場合、デバッグセッション中に起こっていることをすべて見るのは難しいです。ただし、toParseは関数readline()(57行目)への参照によって渡されるため、その関数の本体で値を変更することができます。元の質問で提供PNG画像から

53 if (toParse != "") 
54 { 
55  while (toParse[0] != '{' && toParse[0] != '.' && toParse.IndexOf("//") != 0) 
56  { 
57   nextLine = readLine(ref toParse); 
58   ... 

53行で、toParseは空ではありません。次に、whileループの反復の1つの間、それは空の値に更新されます。これにより、配列インデックスへのアクセス(つまり、while条件のtoParse[0])に例外がスローされます。

C#のrefキーワードの詳細については、this StackOverflow issueまたはthis official Microsoft documentationを参照してください。

こちらがお役に立てば幸いです。

関連する問題