"while"的一个小问题.我怀疑.也许您可以解决它. [英] A little problem with the "while". I susspect it. Maybe you can resolve it.

查看:54
本文介绍了"while"的一个小问题.我怀疑.也许您可以解决它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在代码内.

The question is inside code.

{
string path = Application.StartupPath;
string tt = "";
List<string> e34 = new List<string>();

private void Form1_Load(object sender, EventArgs e)
        {
            sr = new StreamReader(path + "\\mission.scn");
            tt = sr.ReadToEnd(); sr.Close();

            StringSyndicate ss = new StringSyndicate(tt);
            while (!ss.EndOfString) //when is near the EndOfString (one integer left to the end)
            {
                ss.ReadLine();     //it perform this line who increment a private int with 1 = the actual EndOfString (go down and see)
		....
	    }
	label.Text = "[" + e34.Count.ToString() + "]"; //this line is never reach - What is the problem?(I suspect the "while")...

        }
}


 public class StringSyndicate
    {
        public StringSyndicate(string text)
        {
            originaltext = text;
            using (StringReader reader = new StringReader(originaltext))
            {
                string miline;
                while ((miline = reader.ReadLine()) != null)
                {
                    txtlines.Add(miline);
                }
            }
        }
        private static string originaltext = "", newLine = "";
        List<string> txtlines = new List<string>();
        StringReader sr = new StringReader(originaltext);
        int c = -1;



        public string ReadToEnd()
        {
            return originaltext;
        }

        public int LinesInt
        {
            get
            {
                return txtlines.Count;
            }
        }
        public string LinesStr
        {
            get
            {
                return txtlines.Count.ToString();
            }
        }
        int savec = 0;
        public void PositionSave()
        {
            savec = c;
        }
        public void PositionLoad()
        {
            c = savec;
        }

        public string ReadLine()
        {
            c++;			//here it increment the last number
            return txtlines[c];		// but when it reach this line, the compiler do NOT  continue and exit. 
        }
        public string CurrentLine
        {
            get
            {
                return txtlines[c];
            }
        }

        bool endOfString = true;
        public bool EndOfString
        {
            get
            {
                int cnt = originaltext.Length;
                if (originaltext.EndsWith(""))
                {
                    endOfString = false;
                }
                return endOfString;
            }
        }
    }


谢谢

推荐答案

首先,编译器没有对编译程序进行任何操作,也就是说,当您拥有exe时.那么 JIT编译器 [
First of all the compiler does nothing after you''ve compiled your program, that is when you have your exe. Well the JIT compiler[^] does, but just forget about that one, just adding it so noone yells at me :P

the reason your program stops running is because it''s casting an exception sooner or later as you have no way of knowing that there are no more lines. So sooner or later you''ll call ReadLine when c is at txtlines.Count - 1 and thus you index into txtlines[txtlines.Count] which always result in a IndexOutOfRangeException

Well You have alot of problems in your code.
0) Naming c is not a very saying name, I would call it something along the lines of _currentLine you can remove the underscore I use it to emphasize that it''s a field and not a local variable.
LinesStr, is also very badly named, What would be expected is all the lines in one string, it should be named LineCount or something along those lines.

1) In your LinesStr (LineCount) you return the the line count as a string, bad, bad practice. You should change the return type to int and remove .ToString()

2) EndOfString makes no sense it only returns true of the input is an empty string and this is the source of your immediate problem.
You should return c >= txtlines.Count


这篇关于"while"的一个小问题.我怀疑.也许您可以解决它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆