Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey. I'm trying to use .Remove in C# but keep getting the error "Index and count must refer to a location within the string. Parameter name: count."

C#
string UserID = tbxUserID.Text;
string UserIDSort = UserID.Remove(2, 7);
if (UserIDSort == "10")
{
Form1 formA = new Form();
formA.Show();
}
else if (UserIDSort == "15")
{
    Form2 formN = new Form2();
    formN.Show();
}
else if (UserIDSort == "20")
{
Form3 formAf = new Air_Force();
formAf.Show();
}



Thats what my code is like any suggestions as what i'm doing wrong? I've googled the problem however can't seem to find a working solution.
Posted
Updated 2-Mar-11 4:30am
v2

Please see http://msdn.microsoft.com/en-us/library/d8d7z2kk.aspx[^]

You input string must be bigger than what you trying to do. What if I give you a string "A". How would your code handle that?
 
Share this answer
 
Comments
WurmInfinity 2-Mar-11 10:38am    
Yeah I realise what was wrong now. The site I got the code off dident explain that the second digit designated the amount of numbers to be removed. Thanks for the quick response :)
Sergey Alexandrovich Kryukov 2-Mar-11 13:01pm    
Correct, my 5 (at the same time with Henry?)
See my advice.
--SA
Without seeing an example of what tbxUserId.Text actually looks like, this can only be a guess but the error is telling you that the count you are using (7) would require going past the end of the string.

The second parameter in Remove() is a count of characters to delete, NOT the position of the last character to delete, in case you are confused.
 
Share this answer
 
Comments
WurmInfinity 2-Mar-11 10:37am    
Yeah, that was what was confusing me. Thanks! :D
Sergey Alexandrovich Kryukov 2-Mar-11 13:02pm    
Sure, my 5.
See my advice in my Answer.
--SA
You need to code more defensively than that, you are assuming that (2, 7) is always going to refer to a position within your string.

But what if I entered '10ABC' into your tbxUserID? Then the second argument (7) isn't valid and you are going to see the error you're struggling with

I don't know what you're trying to do, are you trying to find a string and remove sections that match?

C#
string UserID = tbxUserID.Text;
string UserIDSort = UserID.Remove(2, UserID.Length - 2);


This way, the length of UserID can change and your calculation is still valid

You could probably just do this with a substring if the code is always 2 long?

C#
UserIdSort = UserID.Substring(0, 2);
 
Share this answer
 
v2
Comments
WurmInfinity 2-Mar-11 10:38am    
Yeah I realise what was wrong now. The site I got the code off dident explain that the second digit designated the amount of numbers to be removed. Thanks for the quick response :)
Sergey Alexandrovich Kryukov 2-Mar-11 13:00pm    
Good answer. My 5. See the advice in mine.
--SA
The rule of thumb is: if you use string.Remove repeatedly on the same string variable or even repeatedly concatenated strings using "+", you should use StringBuilder instead.

Strings are immutable. You never modify a string object using string operation. Instead, you create a brand new string object which is possibly assigned to the same string variable. If you do this over and over, performance suffer. Use StringBuilder for string modification, use StringBuilder.ToString to get back to string type.

—SA
 
Share this answer
 

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