VC# .NET newbie wants to avoid endless loop

Status
Not open for further replies.

Siena1383

Baseband Member
Messages
51
I'm trying to teach myself Visual C# .NET (2003) using a book. One of the early chapters has this exercise:
"Create a project with a form and a text box. Add code to the TextChanged event to cause a recursion when the user types in text. (Hint: Concatenate a character to the end of the user's text using a statement such as this:
txtMyTextBox.Text = String.Concat (this.txtMyTextBox.Text, "a"); )"

If I just add in that code, of course I get an endless loop. So I thought I'd add a Count so it would only add the "a" once. I added this code:

private void H4Ex2txtBox_TextChanged(object sender, System.EventArgs e)
{
//This adds "a" after every text change.
//The count keeps the recursion finite.
const int AddA = 1;
for (int intIndex=0; intIndex < AddA.Count; intIndex++)
H4Ex2txtBox.Text = String.Concat(this. H4Ex2txtBox.Text,"a");
}


I get this error: 'int' does not contain a definition for 'Count'
I figure I'm declaring the constant incorrectly, but I'm just not sure what to do differently.
 
hmm, why use count if you know it's always one?

do you understand why you get an endlees loop with the first snippet? please explain.
 
Looks like there is no "Count" method for an int - which seems reasonable. Try:
Code:
for (int intIndex=0; intIndex < AddA; intIndex++)
instead.

EDIT: though I would like to add; the first line of code doesn't make an infinite loop, not on it's own anyway, and also I don't see any recursion there (nor do I know why it would apply).
 
So I thought I'd add a Count so it would only add the "a" once.
Your 'for' loop will not prevent the recursion. Each time you concatenate the string, the event handler method will be invoked. The for loop will execute every time the method is invoked.
 
Interesting. jaeusm, does the name of that method alone make it an event handler?
No, the event subscription code is not shown. It was more than likely auto-generated using Visual Studio's visual designer since this was an example out of a text book. The subscription code is simple and would look like this:

Code:
txtMyTextBox.TextChanged += H4Ex2txtBox_TextChanged;
 
hmm, why use count if you know it's always one?

do you understand why you get an endlees loop with the first snippet? please explain.

1. I was using Count because I needed a way to allow it do its thing once (add the "a" after each keystroke), then stop until the next keypress.

2. I don't understand what you mean by "the first snippet."
I do know that the TextChanged event is triggered with each keystroke, not just when the user hits Enter to commit the input. But it's also triggered each time the "a" is added. So what that line of code along says is, on a TextChanged event, add an "a" - and that in turn triggers a new TextChanged event, which adds another "a," ad infinitum --> endless loop.
 
Looks like there is no "Count" method for an int - which seems reasonable. Try:
Code:
for (int intIndex=0; intIndex < AddA; intIndex++)
instead.

EDIT: though I would like to add; the first line of code doesn't make an infinite loop, not on it's own anyway, and also I don't see any recursion there (nor do I know why it would apply).

Sounds good, but what is "AddA"? Is that a variable? A method? Don't I have to declare it?

(Sorry, feeling very newbie and stupid here.)
 
Your 'for' loop will not prevent the recursion. Each time you concatenate the string, the event handler method will be invoked. The for loop will execute every time the method is invoked.

Right, what I'm trying to do is derail it before it adds an "a" a second time (and a 3rd and a
4th etc.)
 
Status
Not open for further replies.
Back
Top Bottom