Wednesday, September 02, 2009

I'm been working on a CodeGenerator that creates code for a dll. Doing so I was in need for a Variable Name validator so the created code doesn't contain invalid variables or enums.

This is the function I created:
' Returns True if variable name is valid.
Function ValidateVariable(ByVal inData As String) As Boolean
Dim VbKeyWords() As String = {"AddHandler", "AddressOf", "Alias", "And", "AndAlso", "Ansi", "As", "Assembly", "Auto", "Binary", "Boolean", "ByRef", "Byte", "ByVal", "Call", "Case", "Catch", "CBool", "CByte", "CChar", "CDate", "CDbl", "CDec", "Char", "CInt", "Class", "CLng", "CObj", "Compare", "Const", "Continue", "CSByte", "CShort", "CSng", "CStr", "CType", "CUInt", "CULng", "CUShort", "Custom", "Date", "Decimal", "Declare", "Default", "Delegate", "Dim", "DirectCast", "Do", "Double", "Each", "Else", "ElseIf", "End", "EndIf", "Enum", "Erase", "Error", "Event", "Exit", "Explicit", "ExternalSource", "FALSE", "Finally", "For", "Friend", "Function", "Get", "GetType", "Global", "GoSub", "GoTo", "Handles", "If", "Implements", "Imports", "In", "Inherits", "Integer", "Interface", "Is", "IsFalse", "IsNot", "IsTrue", "Let", "Lib", "Like", "Long", "Loop", "Me", "Mid", "Mod", "Module", "MustInherit", "MustOverride", "MyBase", "MyClass", "Namespace", "Narrowing", "New", "Next", "Not", "Nothing", "NotInheritable", "NotOverridable", "Object", "Of", "Off", "On", "Operator", "Option", "Optional", "Or", "OrElse", "Overloads", "Overridable", "Overrides", "ParamArray", "Partial", "Preserve", "Private", "Property", "Protected", "Public", "RaiseEvent", "ReadOnly", "ReDim", "Region", "REM", "RemoveHandler", "Resume", "Return", "SByte", "Select", "Set", "Shadows", "Shared", "Short", "Single", "Static", "Step", "Stop", "Strict", "String", "Structure", "Sub", "SyncLock", "Text", "Then", "Throw", "To", "True", "Try", "TryCast", "TypeOf", "UInteger", "ULong", "Unicode", "Until", "UShort", "Using", "Variant", "Wend", "When", "While", "Widening", "With", "WithEvents", "WriteOnly", "Xor"}

If Len(inData) = 0 Then Return False 'Empty string


' Chk if 1st character is a numeric
Select Case inData.Chars(0)
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
' Not OK
Return False
End Select


' Chk for allowed characters _ 0-1, a-z, A-Z
Dim i As Integer
For i = 0 To Len(inData) - 1
Select Case Asc(inData.Chars(i))
Case 48 To 57, 65 To 90, 97 To 122 ' 0-9, A-Z, a-z
'OK
Case 95 '_
' Ok With _ but not only as _
If inData.Length = 1 Then Return False
Case Else
Return False
End Select

' Chk for Vb KeyWords
Dim j As Integer
Dim txt As String = LCase(inData)
For j = 0 To UBound(VbKeyWords)
' chk agains Keywords
If txt = LCase(VbKeyWords(j)) Then
Return False
End If
Next
Next
Return True

End Function

No comments:

MySQL to MariaDB

 Here is a good description how to migrate to MariaDb MySQL to MariaDb Example of how to dump data from MySQL port=3307: mysqldump --user=ro...