Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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."
 
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 2-Mar-11 5:29am
Edited 2-Mar-11 5:30am
Yusuf40K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  
Comments
WurmInfinity at 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 :)
SAKryukov at 2-Mar-11 13:01pm
   
Correct, my 5 (at the same time with Henry?)
See my advice.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
WurmInfinity at 2-Mar-11 10:37am
   
Yeah, that was what was confusing me. Thanks! :D
SAKryukov at 2-Mar-11 13:02pm
   
Sure, my 5.
See my advice in my Answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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?
 
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?
 
UserIdSort = UserID.Substring(0, 2);
  Permalink  
v2
Comments
WurmInfinity at 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 :)
SAKryukov at 2-Mar-11 13:00pm
   
Good answer. My 5. See the advice in mine.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,478
1 OriginalGriff 6,516
2 Peter Leow 3,587
3 Zoltán Zörgő 3,406
4 Richard MacCutchan 2,397


Advertise | Privacy | Mobile
Web03 | 2.8.150123.1 | Last Updated 2 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100