C# - Struggling yet again

Associate
Joined
30 Dec 2005
Posts
415
Hey to all.

Just getting back into learning C#, and i've hit this problem.
What it currently does it bring up a message box showing the following:
i is currently 0
i is currently 0
i is currently 0
i is currently 0
i is currently 1
i is currently 1
i is currently 1
i is currently 1
i is currently 2
i is currently 2
i is currently 2
i is currently 2
i is currently 3
i is currently 3
i is currently 3
i is currently 3
i is currently 4
i is currently 4
i is currently 4
i is currently 4
Then it says this:
IndexOutOfRangeException was unhandled
and points to this line:
Code:
userdetails[i] = reader.Value;
I thought this was quite odd, as I set this:
Code:
userdetails = new string[4];
Even though the array is starting at 0, so I would have thought the max value was 3 :confused:

Anyway, here is the code if anyone has the time to help with this one.

Code:
        public messenger()
        {
            InitializeComponent();
            String URLString = "http://www.routehiker.org.uk/api/help/userdetails.php";
            XmlTextReader reader = new XmlTextReader(URLString);
            string [] userdetails;
            userdetails = new string[4];
            int i = 0;
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        userdetails[i] = reader.Value;
                        i++;                        
                    break;
                }
                MessageBox.Show("i is currently " + i);
            }
            Form Other = new responses();
            Other.ShowDialog();
        }

Regards,

Rich
 
Last edited:
Code:
public messenger()
        {
            InitializeComponent();
            String URLString = "http://www.routehiker.org.uk/api/help/userdetails.php";
            XmlTextReader reader = new XmlTextReader(URLString);
            string [] userdetails;
            userdetails = new string[4];
            int i = 0;
            while ((reader.Read() && i < 4))
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Text:
                        userdetails[i] = reader.Value;
                        i++;                        
                    break;
                }
            }
        }

It's just stuck in a loop till I force the application to close! It's looping something but it doesn't make sense.
 
Yes its still doing the iterations of each integer in groups of 4. I think what i'll do is research different methods of parsing XML and start again. This method seems a bit unefficient.
 
Its ok I rebuilt the solution and it now works fine.

What I would really appreciate is if someone could add me on msn - richwillars @ hotm... .com and help me to finish this project. I reckon it would be half an hour's work at max, and I usually learn better by example of others.

If someone could add me on msn, i'd supply the project files and explain any details which are needed.

A summary of what needs doing:

Storing user details
Program should run in the background
Passing variables between forms
Activating some code when a socket being called upon by another computer

And thats it :)


Anyone willing to help?

Regards,
Rich
 
Back
Top Bottom