|
-
May 14th, 2012, 04:50 PM
#36
Re: Script Engine - VB.NET Based
 Originally Posted by TheBarret
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.
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
|