Results 1 to 12 of 12

Thread: How would this work? [Resolved]

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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:
    1. Dim theCounter As Long
    2.  
    3. Function DoSomething() As Long
    4. Randomize
    5. Dim theVariable As Long
    6. Do Until theCounter > 12
    7.     theVariable = Rnd() * 200
    8.     DoSomething
    9.     theCounter = theCounter + 1
    10. Loop
    11. 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

  2. #2
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How would this work?

    theCounter is never reset, so it will pick 14 numbers.

  4. #4

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

  5. #5

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How would this work?

    Quote 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

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. theCounter = theCounter + 1
    2. DoSomething

    Or I have not understood your problem properly!

  7. #7

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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:
    1. Function Execute() As Long
    2. ....
    3. ....
    4. Do Until (currPos > InStr(VBFuncArgsEndPos, theScript, "End Sub", vbTextCompare))
    5.     'loop through and find keywords..
    6.     If (UCase$(Mid$(theScript, currPos, 2) = "If")) Then
    7.     ElseIf (UCase$(Mid$(theScript, currPos, 6) = "End If")) Then
    8.     ElseIf (UCase$(Mid$(theScript, currPos, 3) = "For")) Then
    9.     ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Next")) Then
    10.     [b]ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Call")) Then
    11.         Execute "FunctionName"[/b]
    12.     End If
    13. currPos = currPos + 1
    14. 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

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.

  10. #10

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How would this work?

    Ahh, I got it working. Declaring it static made the script fail.
    VB Code:
    1. 'My Execute function
    2.  
    3. Public Function Execute(theFunction As String) As Boolean
    4. On Error GoTo Err
    5. 'returns true if no errors, false if not..
    6. Dim currPos As Long 'the current position in this function
    7. Select Case Language
    8.     Case VB:
    9.         Dim VBfunc_pos As Long 'position of "Function " & theFunction
    10.         Dim VBFuncArgStartPos As Long 'lol, the opening parenthesis (
    11.         Dim VBFuncArgEndPos As Long 'the closing parenthesis )
    12.         VBfunc_pos = InStr(VBfunc_pos + 1, theScript, "Sub " & theFunction, vbTextCompare)
    13.         VBFuncArgsStartPos = InStr(VBfunc_pos + 1, theScript, "(", vbTextCompare)
    14.         VBFuncArgsEndPos = InStr(VBFuncArgsStartPos + 1, theScript, ")", vbTextCompare)
    15.         currFunction = Mid$(theScript, vbfuncpos + 7, VBFuncArgsStartPos - (VBfunc_pos + 4))
    16.         currPos = VBFuncArgsEndPos
    17.         Do Until (currPos > InStr(VBFuncArgsEndPos, theScript, "End Sub", vbTextCompare))
    18.             'loop through and find keywords..
    19.             If (UCase$(Mid$(theScript, currPos, 2) = "If")) Then
    20.             ElseIf (UCase$(Mid$(theScript, currPos, 6) = "End If")) Then
    21.             ElseIf (UCase$(Mid$(theScript, currPos, 3) = "For")) Then
    22.             ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Next")) Then
    23.             ElseIf (UCase$(Mid$(theScript, currPos, 6) = "MsgBox")) Then
    24.                 MsgBox "BLA", , "MsgBox Call"
    25.             ElseIf (UCase$(Mid$(theScript, currPos, 4) = "Call")) Then
    26.                 'find the function name and execute it..
    27.                 'Execute Mid$(theScript, currPos + 5, (InStr(currPos + 1, theScript, vbCrLf, vbTextCompare) - 2 - (currPos + 5)))
    28.                 Execute Mid$(theScript, currPos + 5, (InStr(currPos + 1, theScript, vbCrLf, vbTextCompare)) - (currPos + 5))
    29.             End If
    30.         currPos = currPos + 1
    31.         Loop
    32.         'MsgBox Chr(34) & currFunction & Chr(34)
    33.     Case JS:
    34. End Select
    35. Err:
    36. Exit Function
    37. 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

  11. #11
    Lively Member CodeBlock's Avatar
    Join Date
    May 2005
    Location
    The_Universe.Milky_Way. Solar_System.Inner_Ring. The_Earth.Asia. India.TN. Chennai. Home_Sweet_Home
    Posts
    85

    Exclamation 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:
    1. currFunction = Mid$(theScript, [b]vbfuncpos[/b] + 7, VBFuncArgsStartPos - (VBfunc_pos + 4))

    is supposed to be:
    VB Code:
    1. currFunction = Mid$(theScript, [b]VBfunc_pos[/b] + 7, VBFuncArgsStartPos - (VBfunc_pos + 4))

    Also, you have declared as:
    VB Code:
    1. Dim VBFuncArgStartPos As Long 'lol, the opening parenthesis (

    and used the var as:
    VB Code:
    1. 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

  12. #12

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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
  •  



Click Here to Expand Forum to Full Width