索引和计数必须引用字符串中的位置.参数名称:count. [英] Index and count must refer to a location within the string. Parameter name: count.

查看:738
本文介绍了索引和计数必须引用字符串中的位置.参数名称:count.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿.我正在尝试使用C.Remove,但仍收到错误消息索引和计数必须引用字符串中的位置.参数名称:count."

 字符串 UserID = tbxUserID.Text;
字符串 UserIDSort = UserID.Remove( 2  7 );
如果(UserIDSort == "  )
{
Form1 formA =  Form();
formA.Show();
}
其他 如果(UserIDSort == "  15")
{
    Form2 formN =  Form2();
    formN.Show();
}
其他 如果(UserIDSort == "  20")
{
Form3 formAf =  Air_Force();
formAf.Show();
} 




多数民众赞成在我的代码是什么像任何建议,因为我做错了吗?我已经搜索了这个问题,但是似乎找不到有效的解决方案.

解决方案

请参阅


没有看到tbxUserId.Text实际外观的示例,这只能是一个猜测,但错误告诉您count您是使用(7)将需要超过字符串的末尾.

Remove()中的第二个参数是要删除的字符数,如果您感到困惑,则不是要删除的最后一个字符的位置.


您需要比这更具有防御性的代码,假设(2,7)总是要引用字符串中的位置.

但是,如果我在您的tbxUserID中输入了"10ABC"怎么办?然后第二个参数(7)无效,您将看到自己正在努力解决的错误

我不知道您要做什么,您是否要查找字符串并删除匹配的部分?

 字符串 UserID = tbxUserID.Text;
字符串 UserIDSort = UserID.Remove( 2 ,UserID.Length- 2 ); 



这样,UserID的长度可以更改,并且您的计算仍然有效

如果代码总是2个长,您可能只用一个子字符串来完成此操作?

 UserIdSort = UserID.Substring( 0  2 );  


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.

解决方案

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?


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.


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);


这篇关于索引和计数必须引用字符串中的位置.参数名称:count.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆