如何解决以下OutofRangeException [英] How to resolve the following OutofRangeException

查看:74
本文介绍了如何解决以下OutofRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在遵循在WPF应用程序中使用以下语句的声明,

"ArgumentOutOfRangeException未由用户代码处理"

在我的Print.Xaml.cs

我有TextBox,需要将其从C#(.Net )传递给托管代码(C ++/CLI)

Hi All,

I am getting following assertion on using the below statement in WPF Application,

"ArgumentOutOfRangeException was unhandled by user code"

In my Print.Xaml.cs

I have TextBox and need to pass the same to managed code(C++/CLI) from C#(.Net)

private UInt32 _ValueCntTextBox = 0;
//.....

private void UpdatePrintInfo()
{

line 1: int value = PrintInfoTextBlk.Text.IndexOf(':');
line 2: string mystring = PrintInfoTextBlk.Text.Substring(value +1,PrintInfoTextBlk.Text.IndexOf(')' )-value -1);
line 3: ValueCntTextBox.Text = value;
line 4: UInt32.Parse(value,out _ValueCntTextBox);

...
...
}


上面的异常是在第2行获得的:
解决上述问题的任何帮助都非常可观.

我正在使用Windows 7 O/S,VS2008 IDE
问候,
Samanth_90


The above Exception is obtained @ line 2:
Any Help in resolving the above is much appreciable.

I am using Windows 7 O/S, VS2008 IDE
With Regards,
Samanth_90

推荐答案

PrintInfoTextBlk.Text.Substring()调用中的参数值是什么?例如,value包含什么,PrintInfoTextBlk.Text.IndexOf('')'' )返回什么?
What is the value of the parameters in the PrintInfoTextBlk.Text.Substring() call? For example what does value contain, and what does PrintInfoTextBlk.Text.IndexOf('')'' ) return?


从上面的代码中,我想您正在尝试提取:"和"'')''.

我会这样编码:
From the code above, I suppose you are trying to extract the text portion between '':'' and '')''.

I would code like:
// save text to avoid accessing underlying control multiple times
string sText = PrintInfoTextBlk.Text;
// save position of ':' if present; -1 otherwise!
int nStartIndex = sText.IndexOf(':');
// save position of ')' if present; -1 otherwise!
int nEndIndex = sText.IndexOf(')');

string mystring = null; // null will later tell that no valid chars have been found
					
// Both chars were found, so extract substring
if ((nStartIndex >= 0) && (nEndIndex > nStartIndex))
    mystring = sText.Substring(nStartIndex + 1, nEndIndex - nStartIndex - 1);
// Only ':' was found, so extract everything until end of text
else if ((nStartIndex >= 0) && (nEndIndex < 0))
    mystring = sText.Substring(nStartIndex + 1);

ValueCntTextBox.Text = nStartIndex;
UInt32.Parse(nStartIndex, out _ValueCntTextBox);


这篇关于如何解决以下OutofRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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