Question How do read part of a string in a conditional statement

FroDesigns

Member
Joined
Aug 23, 2009
Messages
6
Programming Experience
1-3
I am trying to make a programming language and cant figure out how read the part of the string I want in a conditional statement.
Ive tried this:
VB.NET:
If (syn_(0) = "p" & syn_(1) = "u" & syn_(2) = "t") Then
'code to do
End If
But It comes up with an error. IS there any other way because I don't want to have to come up with syntax that is all the same length.

Any help is appriciated
 
The & symbol is used as a logical operator in C#. In Visual Basic, it's used for concatenation. What you want is the Visual Basic logical operator: And


If (syn_(0) = "p" And syn_(1) = "u" And syn_(2) = "t") Then
'etc.
 
So I replaced all the "&'s" with "And's" but when I added more commands and tried to use them in the prorgam it came up with this:
Index was outside the bounds of the array.

Now this is my code:
VB.NET:
1:
        mainValue = Console.ReadLine
        mainValue = mainValue.ToLower.ToString
        mv = mainValue
        charNum = mv.Length
        If (mv(0) = "p" And mv(1) = "r" And mv(2) = "i" And mv(3) = "n" And mv(4) = "t") Then
            inputA = mv.Trim(mv(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            charNum = inputA.Length
            inputA = inputA.Trim("'")
            Console.WriteLine(inputA)
        ElseIf (mv(0) = "/" And mv(1) = "/") Then

        Else
            Console.WriteLine("error")
            Console.WriteLine("'" & mv & "' is not a valid command")
        End If

        GoTo 1
 
You never even declared the array!!! Did you do:

Dim mv(4) As String

?

Why do you keep repeating:

inputA = inputA.Trim(inputA(0))

?

And why in Heaven's name are you using GoTo??? That is totally obsolete. Are you coming from GWBasic or QBasic? It also looks like you have created an endless loop.

First and foremost, place Option Strict On at the top of your code. That will force you to use correct variable types and avoid a lot of errors.

Next, I suggest you get yourself a good book on Visual Basic for beginners or run a tutorial. You need to learn how to use structured blocks, not GoTos.

Exactly what is your program supposed to do?
 
Well im trying to make a command line interpreter.
So I switched the "GoTo 1" statement to "controls()" (my subroutine that contains everything) I added "Option String On" and I added arrays for the different commands. This is my code:
VB.NET:
    Sub Main()
        Console.Title = "Emerald Command Line Interpreter"
        Console.WriteLine("Emerald 1.0 Command Line Interpreter")
        controls()
    End Sub
    Sub controls()

        mainValue = Console.ReadLine
        mainValue = mainValue.ToLower.ToString
        mv = mainValue
        charNum = mv.Length
        print(0) = "p"
        print(1) = "r"
        print(2) = "n"
        print(3) = "t"
        comment(0) = "/"
        comment(1) = "/"

        'If (mv(0) = "p" And mv(1) = "r" And mv(2) = "i" And mv(3) = "n" And mv(4) = "t") Then
        If mv = print(3) Then
            syn_ = mv.ToCharArray

            inputA = mv.Trim(mv(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            inputA = inputA.Trim(inputA(0))
            charNum = inputA.Length
            inputA = inputA.TrimEnd()
            Console.WriteLine(inputA)
        ElseIf mv = comment(1) Then

        Else
            Console.WriteLine("error")
            Console.WriteLine("'" & mv & "' is not a valid command")
        End If

        controls()
    End Sub

But the problem is that now I cant read certain the characters in the string separately . And is there a way to split the string where it finds a specific character?
I tried using string.split but when I run the program it doesn't display the text I wrote in the single quotes. It goes to my error message
 
Last edited:
Back
Top