|
-
May 4th, 2005, 03:56 AM
#1
Thread Starter
G&G Moderator
How would this work? [Resolved]
Howdy all,
I'm wondering how Visual Basic (and probably, compilers in general) would handle a situation like this.
Say I had this:
VB Code:
Dim theCounter As Long
Function DoSomething() As Long
Randomize
Dim theVariable As Long
Do Until theCounter > 12
theVariable = Rnd() * 200
DoSomething
theCounter = theCounter + 1
Loop
End Function
Its kind of hard to explain. If theVariable is set to a random number (in our case, say 100) on our first call, and then DoSomething is called again, another theVariable is declared, and set to a random number. Once this has all finished, would the variables be reset to their original values once the calling has been returned? I don't really know how to explain it. I'll try and draw up a diagram of what I mean. I just want to know before I try anything.
Thanks in advance,
Phreak
Last edited by «°°phReAk°°»; May 4th, 2005 at 05:38 AM.
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 04:05 AM
#2
KING BODWAD XXI
Re: How would this work?
If the sub is recalled the variable will be created exclusively for the new call. They never mix.
P.s If you call a function too many times within itself you get an out of stack space error so be careful
-
May 4th, 2005, 04:09 AM
#3
Re: How would this work?
theCounter is never reset, so it will pick 14 numbers.
-
May 4th, 2005, 04:10 AM
#4
Thread Starter
G&G Moderator
Re: How would this work?
Reason I ask is, I'm creating a small scripting language for my application. I'm parsing it by moving 1 character at a time within a loop, and searching for keywords, then adding the difference (to make it faster, sort of). For a function call, I was wondering if I could just call my Execute function again, to execute the function, then come back to the calling function.
Would it be possible as long as function calls didn't occur > say, 10 times?
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 04:11 AM
#5
Thread Starter
G&G Moderator
Re: How would this work?
 Originally Posted by dglienna
theCounter is never reset, so it will pick 14 numbers.
theCounter was just so the loop didn't go on forever. I just wonder if theVariable would be set, called again and set, then removed and set back to its original value.
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 04:42 AM
#6
Re: How would this work?
The code would get stuck in an infinite loop and ultimately give error "Out of Stack Space". The terminating condition "theCounter = theCounter + 1" will never be executed because it is after the function call "DoSomething"
Instead you will have to code it like this
VB Code:
theCounter = theCounter + 1
DoSomething
Or I have not understood your problem properly!
-
May 4th, 2005, 04:56 AM
#7
Thread Starter
G&G Moderator
Re: How would this work?
Oh my. I did miss that. Thank you Pradeep 
I suppose I'm really looking for a way to parse my script. I want to be able to call functions while executing another function. So, I want to be able to execute code, stop, execute other code, then come back. Anyone know of a good way to parse scripts or links to examples?
This is basically what I have so far..
VB Code:
Function Execute() As Long
....
....
Do Until (currPos > InStr(VBFuncArgsEndPos, theScript, "End Sub", vbTextCompare))
'loop through and find keywords..
If (UCase$(Mid$(theScript, currPos, 2) = "If")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 6) = "End If")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 3) = "For")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Next")) Then
[b]ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Call")) Then
Execute "FunctionName"[/b]
End If
currPos = currPos + 1
Loop
The bold bit is what I am wondering. Would currPos be reset to the first "End Sub" position when it returned?
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 05:07 AM
#8
Re: How would this work?
Declare it STATIC to the module.
Incidentally, I missed that DoSomething was the name of the module.
I assumed that it was a comment to just DO SOMETHING...
Last edited by dglienna; May 4th, 2005 at 05:30 AM.
-
May 4th, 2005, 05:32 AM
#9
Re: How would this work?
Its the same thing !
It would get stuck to the first "Call" it encounters and would be never come out of it. It won't be reset to any of the "End Sub"s. Once it encounters the "Call" word, it won't be able to increment the counter (currPos) any furthur.
Edit: You should add something to increment it before the call to Execute function to avoid this.
Last edited by Pradeep1210; May 4th, 2005 at 05:36 AM.
-
May 4th, 2005, 05:36 AM
#10
Thread Starter
G&G Moderator
Re: How would this work?
Ahh, I got it working. Declaring it static made the script fail.
VB Code:
'My Execute function
Public Function Execute(theFunction As String) As Boolean
On Error GoTo Err
'returns true if no errors, false if not..
Dim currPos As Long 'the current position in this function
Select Case Language
Case VB:
Dim VBfunc_pos As Long 'position of "Function " & theFunction
Dim VBFuncArgStartPos As Long 'lol, the opening parenthesis (
Dim VBFuncArgEndPos As Long 'the closing parenthesis )
VBfunc_pos = InStr(VBfunc_pos + 1, theScript, "Sub " & theFunction, vbTextCompare)
VBFuncArgsStartPos = InStr(VBfunc_pos + 1, theScript, "(", vbTextCompare)
VBFuncArgsEndPos = InStr(VBFuncArgsStartPos + 1, theScript, ")", vbTextCompare)
currFunction = Mid$(theScript, vbfuncpos + 7, VBFuncArgsStartPos - (VBfunc_pos + 4))
currPos = VBFuncArgsEndPos
Do Until (currPos > InStr(VBFuncArgsEndPos, theScript, "End Sub", vbTextCompare))
'loop through and find keywords..
If (UCase$(Mid$(theScript, currPos, 2) = "If")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 6) = "End If")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 3) = "For")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Next")) Then
ElseIf (UCase$(Mid$(theScript, currPos, 6) = "MsgBox")) Then
MsgBox "BLA", , "MsgBox Call"
ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Call")) Then
'find the function name and execute it..
'Execute Mid$(theScript, currPos + 5, (InStr(currPos + 1, theScript, vbCrLf, vbTextCompare) - 2 - (currPos + 5)))
Execute Mid$(theScript, currPos + 5, (InStr(currPos + 1, theScript, vbCrLf, vbTextCompare)) - (currPos + 5))
End If
currPos = currPos + 1
Loop
'MsgBox Chr(34) & currFunction & Chr(34)
Case JS:
End Select
Err:
Exit Function
End Function
..that works with a script like this:
Code:
'Test Script
Dim Blah
Sub Main()
Call MyOtherSub
MsgBox
End Sub
Sub MyOtherSub()
MsgBox
End Sub
Thanks everyone 
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
May 4th, 2005, 06:43 AM
#11
Lively Member
Re: How would this work? [Resolved]
Hello PhreaK,
Don't worry, you are on the right track. Why dont you use the "F8" Key to see how it all happens. But Before that please note that you have some typo mistakes in your code (Thats where the "Option Explicit" comes handy).
VB Code:
currFunction = Mid$(theScript, [b]vbfuncpos[/b] + 7, VBFuncArgsStartPos - (VBfunc_pos + 4))
is supposed to be:
VB Code:
currFunction = Mid$(theScript, [b]VBfunc_pos[/b] + 7, VBFuncArgsStartPos - (VBfunc_pos + 4))
Also, you have declared as:
VB Code:
Dim VBFuncArgStartPos As Long 'lol, the opening parenthesis (
and used the var as:
VB Code:
VBFuncArg[b]s[/b]StartPos = InStr(VBfunc_pos + 1, theScript, "(", vbTextCompare)
Note the extra 's' which is missing in your declaration.
So, for God's sake place the "Option Explicit" line on top of your module (the code view is always called a Module regardless of whether it is from a Form or a Class or a stand-alone Module)
You will end up in the stack only when your script code is like this:
Code:
Sub Main()
Call foo1
End Sub
Sub foo1()
Call foo2
End Sub
Sub foo2()
Call foo3
End Sub
Sub foo3()
Call foo4
End Sub
Sub foo4()
Call foo5
End Sub
Sub foo5()
Call foo6
End Sub
..etc till the stack limit
or a script like this:
Code:
Sub Main()
Call foo1
End Sub
Sub foo1()
Call foo2
End Sub
Sub foo2()
Call foo1
End Sub
So, your code will not run out of stack space normally. Place your "currPos = currPos + 1" intact.
And again, i insist you on stepping over your code with your F8 key and see it yourself how the program loop goes 
I will continue my post after some research on your script,
Sorry, i gtg, thats why
BRB
HTH
Neo
--
My Quote: "I will be back! Get ready" :P
-
May 4th, 2005, 06:50 AM
#12
Thread Starter
G&G Moderator
Re: How would this work? [Resolved]
Oh my god. It worked, but must have declared new variables as variants and used the old ones. Meh, thanks a heap CodeBlock. /me always forgets Option Explicit. I used to always use it, and Option Base. Meh, anyway, thanks again,
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|