Page 2 of 2 FirstFirst 12
Results 41 to 45 of 45

Thread: Script Engine - VB.NET Based

  1. #41
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by TheBarret View Post
    Was not about hurting feelings more about manors.
    I don't get it? What's been wrong with my manor? I have done nothing but trying to help you in this thread and when I mentioned that I have a hard time following spaghetti code which GOTO's lead to, you took that as an insult. It was not meant as such, I just want you to understand good coding practices.

    Over to your code:

    If you're still talking about how to avoid Goto statements, then this is one way of doing it:
    Code:
    Private Function CheckForVar(strline As String) As String
        ' Changed the name of all local variables to be
        ' camelCased instead of PascalCased (leading lower case character)
        Dim tmpLine As String = strline
        Dim currentChar As String = ""
        Dim varName As String = ""
        Dim varValue As String = ""
        Dim pos As Integer = 0
        Dim posT As Integer = Len(strline)
        Dim check As Boolean = False
        Dim done As Boolean = False ' <-- I added this
    
        Do  ' <-- Replaced your Label with a Do loop
            For i = 0 To PosT - 1 ' <-- Change the For loop so it loops from 0. This is for SubString to work correctly
                currentChar = tmpLine.SubString(i, 1) '<-- Changed the MID call to .Net SubString
                If (currentChar = TABLE_VARIABLE_SYNTAX) Then
                    If (check = False) Then
                        check = True
                        pos = i + 1 '<--- Added 1 since I changed the loop to be zero based
                    Else
                        RaiseError("Unexpected variable symbol")
                        Return ""
                        'Removed the Exit Function since it will never be reached
                    End If
                End If
                If (check = True) Then
                    If (currentChar = " ") Then
                        varName = Replace(varName, TABLE_VARIABLE_SYNTAX, "")
                        varValue = Variables.GetVar(VarName)
                        tmpLine = RebuildString(tmpLine, varValue, pos, pos + Len(varName))
                        check = False
                        varName = ""
                        varValue = ""
                        done = False '<-- Added this
                        Exit For ' <-- Instead of a GOTO simply Exit the For loop
                    End If
                    If (i = posT - 1) Then ' <-- Added -1 since For loop is zero based
                        varName = Replace(varName, TABLE_VARIABLE_SYNTAX, "")
                        varValue = Variables.GetVar(varName)
                        tmpLine = RebuildString(tmpLine, varValue, pos, pos + Len(varName))
                        check = False
                        varName = ""
                    End If
                End If
        
                If (check = True) Then
                    varName = varName & currentChar
                End If
                done = True ' <--- Added this. This will never be set if we exit the For loop
            Next
        Loop Until done '<-- Added this
    
        Return tmpLine
    End Function
    I put in comment for the changes I made but the main changes is that the code block is now inside a Do ... Loop Until loop instead of having a label and a GOTO statement.

    I also changed your Mid call since that is an older function from VB6, a more ".Net way" of doing it is to call the String.SubString method.

    I also changed the casing of all your local variables. It is a de facto standard in .Net to use camelCasing (first character in lower case) for local variables and PascalCase (first character in upper case) for methods, properties, and public fields.

    I also removed a Exit Function line since you had a Return statement on the line before it. As soon as you hit a return the sub/function will exit. So the Exit Function was just redundant and you would never reach it.

    I couldn't of course test this since I don't have all the necessary code but I hope I haven't introduced any errors.
    Last edited by Joacim Andersson; May 14th, 2012 at 04:55 PM.

  2. #42

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by Joacim Andersson View Post
    End Function[/code]I put in comment for the changes I made but the main changes is that the code block is now inside a Do ... Loop Until loop instead of having a label and a GOTO statement.

    I also changed your Mid call since that is an older function from VB6, a more ".Net way" of doing it is to call the String.SubString method.

    I also changed the casing of all your local variables. It is a de facto standard in .Net to use camelCasing (first character in lower case) for local variables and PascalCase (first character in upper case) for methods, properties, and public fields.

    I also removed a Exit Function line since you had a Return statement on the line before it. As soon as you hit a return the sub/function will exit. So the Exit Function was just redundant and you would never reach it.

    I couldn't of course test this since I don't have all the necessary code but I hope I haven't introduced any errors.
    Okay this is indeed the better "other" solution, i appreciate this example, once this is seen i'd never go bad again. Thanks J.

    -TB

  3. #43
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Script Engine - VB.NET Based

    The 'Goto is bad' is really a rule. Generally, the use of goto implies a programmer with limited experience, is unwilling to listen, is simply resting on their laurels, or all of the above.

    Here's an example of using GOTO statements on a common algorithm:

    Code:
    Public Sub DoSomething()
        Dim a() As Long = New Long() {6, 9, 2, 8, 1, 12, 5, 4, 0, 3, 2, 6, 4}
        Call DoSomething(a)
        Dim i As Integer = 0
    12:
        Debug.WriteLine(a(i))
        i = i + 1
        If i < a.Length Then GoTo 12
      End Sub
    
      Public Sub DoSomething(ByVal a() As Long)
        Dim i As Integer = a.Length - 1
        Dim j As Integer = 0
    a4:
        If a(j) > a(j + 1) Then GoTo 3
        j = j + 1
        If j = i Then GoTo 9
        GoTo a4
    9:
        j = 0
        i = i - 1
        If i = 0 Then GoTo 5
    7:
        GoTo a4
    3:
        Dim t As Long
        t = a(j)
        a(j) = a(j + 1)
        a(j + 1) = t
        GoTo 7
    5:
      End Sub
    As you can see, quite clear and concise. I would say that it's a pretty good example of 'bad' code. Feel free to copy it...maybe find the bug.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  4. #44

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Script Engine - VB.NET Based

    Quote Originally Posted by SJWhiteley View Post
    The 'Goto is bad' is really a rule. Generally, the use of goto implies a programmer with limited experience, is unwilling to listen, is simply resting on their laurels, or all of the above.
    [/CODE]

    As you can see, quite clear and concise. I would say that it's a pretty good example of 'bad' code. Feel free to copy it...maybe find the bug.


    Prob all right but not of use to me atm because this has not very much todo with the project i am doing.

  5. #45

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    57

    Re: Script Engine - VB.NET Based

    Replaced alote since last time, heres what i did.

    - Parser replaced with a lexer (so much easier!)
    - Functions accept parameters instead of compound variables
    - Functions have theire own variable tables
    - Global variables
    - Do-loops (until/while/inf) statement are using the same comparision class as the if-then-else
    - if-then-else supports (true/false or 1/0 or simple comparision methods)
    - Line seperators multiple statements in one line
    - Self made functions automaticly become a function within the engine and can be called from anywhere
    - and alote of fine tuning...

    Screen shot:

Page 2 of 2 FirstFirst 12

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