Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

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

...
...
}

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
Posted
Updated 2-Apr-12 6:04am
v2

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?
 
Share this answer
 
From the code above, I suppose you are trying to extract the text portion between ':' and ')'.

I would code like:
C#
// 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);
 
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