Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeating a function

Status
Not open for further replies.

mancroft

Programmer
Oct 26, 2002
267
GB

Hello

I am trying to get a function to repeat itself a number of times but neither way works.

What is the best way to do this?

Thanks

DETAILS...

I tried

Code:
dim thecounter as Integer = 1 

while thecounter <100

theRepeatingFunction()

thecounter +=1

end while

and also at the end of the Try, Catch Finally

Code:
Private Sub theRepeatingFunction

        Try

            'trythis

        Catch xyz As Exception

            'if error

        Finally

            'what it does
            
            'make it do it again
            theRepeatingFunction()

        End Try

    End Sub
 
if it's a function, isn't it supposed to return something? If yes then maybe you should have a variable receiving the value.

like:

Code:
'imagine you have function returning 1
private function return1() as integer
return 1
end function

private sub testFunction()
dim i as integer 
dim myVar as integer
msgbox(myVar)'intitial value
for i = 0 to 100
myVar += return1()
next
msgbox(myVar)'final value
end sub

the same thing with as sub would be:

Code:
dim i as integer
dim myVar as integer

public sub doTheThing()
myVar += 1

for i = 1 to 100
doTheThing()
next
msgbox(myVar)'you will only see the final value
end sub


both these examples should run.

If this doesn't answer your question the say it and try to be a little more precise on what you really want to do, if you can.
 
Thank you for your help.

After stepping through the code, I have discovered that the problem appears not to be in the function as such.

I am trying to display the output of the function in a label but it appears that it won't display anything (even though the values are correct) until it hits the End Sub when things appear as they should. (See code below.)

Is there a way to get the Sub to repeat itself without pressing the button?

Sorry to be so dense... I am new to VB.net

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  thefunction()

End Sub
 
so you want to actually see the labels value changing as it cycles through the loop?

try putting
MyLabel.text = "Whatever"
MyLabel.parent.refresh()
where you want the value to be shown
 
No problem, this forum is a place where people are eager to help each other.

Well, the label is supposed to display something as soon as it's text property is assigned a string value:

Code:
label1.Text = someString


About the Sub repeting itself, something must decide how many times or untill which condition it must repete itself.

Something in your code must decide for it,

let's say you have a loop:

Code:
dim i as integer
for i = 1 to 100
theFunction()
next

now you can decide to execute the loop when you click on button1:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

dim i as integer
for i = 1 to 100
theFunction()
next

End Sub

Or when your windows form loads:

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dim i as integer
for i = 1 to 100
theFunction()
next

End Sub


but something must trigger the whole process.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top