Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 7th, 2007, 2:41 PM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 41
Rep Power: 0 csrocker101 is on a distinguished road
Throwing an exception when using string constructor

Alrite guys this is my last post and I will stop polluting this C# forum with stupid questions. Basically my client sends a string to server and if there is a space in the string you update the database. What I am trying to do is using the .IndexOf() function and I am assigning everything before the space to string called username and the characters after the space to a string called location. the username variable works just fine. But my location string is not working and throws an exception everytime. Here is my Code:
 public void doRequest()
        {
            
            //streamReader reads incoming connection from client
            StreamWriter sw = new StreamWriter(socketStream);
            StreamReader sr = new StreamReader(socketStream);
            //writes in coming data
           //Assigns data from the streamreader to a string
            string clientInfo = sr.ReadLine();
            int pos = clientInfo.IndexOf(" ");
            
            //assigns string info from stream reader to a char
            char[] newInfo = new char[clientInfo.Length];
            
            //for loop converts each string character to a char character
            for (int i = 0; i < clientInfo.Length; i++)
            {
                newInfo[i] = Convert.ToChar(clientInfo[i]);
            }
            
            //uses the string constructor to assign everything after .IndexOf(" ")
            //and to the end of the array to location 
            string location = new string(newInfo, pos + 1, clientInfo.Length);
            
            //assigns everything from the start of the array to the .IndexOf(" ") 
            //to username
            string username = new string(newInfo, 0, pos);

Where I have string location it throws an exception EVERYTIME which says "Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: startIndex"}" The string username assigns nad works just fine. Any suggestions??! and I'd once again like to thank everyone for their help.
csrocker101 is offline   Reply With Quote
Old Apr 7th, 2007, 3:02 PM   #2
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Can you give us the stack trace? And what are the values of the variable at the line that is throwing the exception? Are the strings being properly encoded and decoded when going across the socket?

I suspect that a little use of debugging utilities or logging what is happening will help you find your problem.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Apr 8th, 2007, 9:59 AM   #3
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 41
Rep Power: 0 csrocker101 is on a distinguished road
The stream is coming in just fine and my char array has exactly the information I want. IT works just fine when you use

string location = new string(newInfo, 0, clientInfo.Length);

But if you put anything other than 0 as the index, it throws an exception! stilll don't know why!
csrocker101 is offline   Reply With Quote
Old Apr 8th, 2007, 2:04 PM   #4
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 41
Rep Power: 0 csrocker101 is on a distinguished road
I found the problem. For some reason it was adding an additional index with the IndexOf(" ") function so to solve it. I just did:

string location = new string(newInfo, 0, clientInfo.Length - pos);

and it works just fine now. odd! but if it works it works
csrocker101 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Could some please explain classes to me... TCStyle C++ 10 Feb 20th, 2006 3:51 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 5:45 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC