Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
            }
        }
    }

thanks
Posted
Updated 4-Oct-11 16:14pm
v4

1 solution

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
 
Share this answer
 
Comments
_Q12_ 4-Oct-11 22:25pm    
the 0 problem:
the c is for "count" word. This project is personal, so no other must keep track of it, or because this class is so little, it can accept some shortcuts.
the 1 problem:
there are 2 properties there :
public int LinesInt //this one is doing the right thing
public string LinesStr //this is very much of help in debugging time... and also is shortening some code.
the 2 problem:
the [c >= txtlines.Count] is making no significant good - right now, still I have the same "IndexOutOfRangeException", at the same spot as before. And no warning...
Simon Bang Terkildsen 4-Oct-11 22:40pm    
0) Well you posted your code here is the code was not private.. and take such silly shortcuts when you write your "private" code you will do it if you ever/when you get a job, as you're used to it and is pressured to get your work done.
In short because no one should ever see this code is no excuse for bad naming or formatting.

1) LinesStr wont do anything for you that LinesInt can't, it's bad practise (and naming) and gain if you do such now and continue to do it you'll do it if/when you get a job.

2) Yes because your loop invariant is EndOfString which is not false when it should be as you increment c in ReadLine, so you would have to write c >= txtlines.Count -1

If you want to become a professional developer, and not have to look for a new job every 3 months or so, you should take to heart what I wrote about naming and formatting. Writing beautiful code readable code is not easy to do and not something that can be learned/taught it comes through practice and experience.
You can of course also just disregard what I said and tell me I'm a jerk.
_Q12_ 4-Oct-11 22:56pm    
I am conscientious about these rules... they are not new to me :)
And you are right, pointing them to me... I will try to do my best in the future.
Oh, about taking a job in this domain...im not so sure i will ever do such a step,because at the base I will still remain an amateur. My qualification is diametrical to programming - i do this from pleasure only (too many words).
The "c >= txtlines.Count -1", now is doing what it should. Well done and thank you v. much for your support.
I am a bit angry because I did not see it(the -1 part). ;) Keep the good work.
Sergey Alexandrovich Kryukov 5-Oct-11 2:48am    
Good review, excellent discussion, my 5.
--SA
Simon Bang Terkildsen 5-Oct-11 9:36am    
Thank you, SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900