[VISUAL BASIC] [NEED HELP] I get an error that I don't want

Started by stripe103, September 09, 2010, 01:02:17 pm

Previous topic - Next topic

stripe103

So, on my school we have started with Visual Basic and I found it quite fun so I'd thought I'll continue on it. Right now I have a project that I hope is going to be a better version of the Windows Command Prompt(will probably never get finished, but you never know).

Anyway, when I put in a command in the input box and hit enter it separates the parts of the command. For example when I write "test command with parameters" it split them up to "test", "command", "with", "parameters". So if I have it so it checks if there is written "command" after "test" it works, but if I just writes "test" I get an error that it is checking for a variable that doesn't exist.

It is probably easier to just post the code so here it is
    ' Initsialize local variables and arrays
        Dim input As String
        Dim SeperateInput() As String
        ' Set "input" variable to input value
        input = Me.input.Text
        ' If Input is empty
        If Me.input.Text = "" Then
        Else
            ' Split up input to parts, seperated by a space character
            SeperateInput = Split(CStr(input), " ")
            ' Set input box to empty
            Me.input.Text = vbNullString
            ' Add a new command line to the output box
            Me.output.Text = output.Text & vbNewLine & ">" & input

            ' This is all available commands

            If SeperateInput(0) = "cls" Then
                Me.output.Text = vbNullString
            End If
            If SeperateInput(0) = "exit" Then
                If SeperateInput(1) = "/b" Then         #This is just an example but it is here the error is. But only when don't write anything more than exit.
                    Me.output.Text = "TEST"
                End If
                Close()
            End If
            If SeperateInput(0) = "about" Then
                Me.output.Text = vbNullString
                Me.output.Text = "Terminal Command Prompt" & vbNewLine & "Made by Markus Rosensköld"
            End If


    End If


I guess it is the array(or what you call it in VB) that is the problem. That it checks for text in an array-part that isn't there, but is there any way to fix it?

I have only been working with Visual Basic for like 4 days so please bear with me.


Ryex

QuoteThis is just an example but it is here the error is. But only when don't write anything more than exit.

and there you have it. if you only write exit then obviously the split function returns an array with only one entry thus checking for SeperateInput(1) will return an error as it doesn't exist.
what you should do is check the length of the SeperateInput array and only if it's length is longer than 1 check SeperateInput(1)
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

stripe103

Working like a charm. Thank you.

Another question. I am making a program where you can add, remove users and change passwords and options on every account without admin access.
What I want to know is how do you change users. Add, remove them, change their password, show all users, show user information and change user options such as country code or full name.