Reputation: 133
So i want to perform some code N times. N is textbox's value (e.g: 12).
I have no idea how to accomplish this, but something like this is on my mind:
For Each i as 1 in textbox1.text
'some code
Next
or
dim num1 as integer = 0
While num1 < textbox1.text
'some code
num1 += 1
Next
Those were just some ideas that were on my mind when i though about this question, none of above is tested nor tried to code.
Any ideas?
Upvotes: 2
Views: 766
Reputation: 31
This might work, I haven't tried:
Dim repeat_times as Integer = 0
Do Until repeat_times = 10 'times you want to repeat
repeat_times = repeat_times + 1
Console.WriteLine("Hello World") 'or anything you want
Loop
Upvotes: 0
Reputation: 38875
First and foremost, turn on Option Strict
. This will save you time in the long run by converting possible runtime errors into compiler errors. At the top of the code file:
Option Strict On
It can also be set for the entire project:
Project Properties -> Compile Tab -> Option Strict selector: On
You can also make it the default for all new projects via:
Tools -> Options -> Projects and Solutions -> VB Defaults
TextBox.Text
always contains text (hence the clever names). "12"
is just a string and is not the same as 12
. So, before you can use it, you need to convert from string to integer. Note: If you want to restrict user input to numbers consider using a NumericUpDown
.
There are several ways to convert, but considering that the data comes from a user, we have to allow that they may enter "I like pie"
instead of numerals. So, we want to use the safest method which in this case is Integer.TryParse
:
' declare int var to hold the result
Dim LoopCount As Integer
If Integer.TryParse(TextBox132.Text, LoopCount) Then
' success! LoopCOunt contains the integer value of the Text control
For n As Integer = 0 To LoopCount - 1
' some code to do something
Next
Else
' failed - scold the user with a MessageBox
End if
The comments in the code explain, but first you declare an integer variable to hold the converted value. Integer.TryParse
is a function which will return True/False
. If it succeeds, your variable will hold the integer value.
See Converting DataTypes for Option Strict for other convert methods and cases where they are appropriate.
Upvotes: 3
Reputation: 133
For n As Int32 = 0 To TextBox1.text - 1
was an answer for my question.
Upvotes: -1