Question Dim, String and Textboxes!

konkor

Member
Joined
Jul 27, 2011
Messages
7
Programming Experience
1-3
I am trying to make a virtual help assistant in my program, something you can type errors or questions to and it answers them. The way I have done this is by making a dim that dims a word and then I give the dimmed word a load of words that might match it, then I added these words up to form sentences. A quick preview is below, but all I want to do is check the sentence against the textbox.text so for example if I done the following:

VB.NET:
'This is the place to Dim words!
        Dim what As String
        Dim are As String
        Dim you As String

        'This is the place to Dim sentences!
        Dim whatareyou As String

        'Complete the words as follows: Dim = "Words"...
        what = "what" & "wat" & "wot" & "wut"
        are = "are" & "r" & "ar" & "arr" & "aar"
        you = "you" & "u" & "yu" & "yuu" & "uu"

        'Complete the sentences as follows: Sentence = Words...
        whatareyou = what & are & you

So you see I have added a few words that people might use to the dim function, so I am hoping to achieve if someone types "wat r u" then it comes back with the proper word which is what, by adding all 3 words to the "whatareyou" dim for the sentence I want to check this against the text box. Basically I want to search the textbox for any of these words and try to figure out what the user has asked. And then reply to their question like below:

VB.NET:
        If txtSend.Text = whatareyou Then
            txtRead.Text = txtRead.Text & vbCrLf & "User: " & txtSend.Text & vbCrLf & "Help: I am a help assistant."
        Else
            txtRead.Text = txtRead.Text & vbCrLf & "User: " & txtSend.Text & vbCrLf & "Help: Sorry what did you say?"
            txtSend.Text = ""
        End If

txtSend is the name for the box were the user types their question, txtRead is the main box that displays the conversation and the button is just a normal control name. Any help with this would be fantastic, thanks!

I am kind of a noob at VB btw and I dont really use the Dim function as much as I probably should do, but I am guessing string is the wrong type to use?
 
Since you're dealing with text, you are correct that you'd need to use strings. However the strings you have won't work. Essentially the & sign means to stick the second string onto the end of the first.

When you run the first section

the string you've called "what" will read "whatwatwotwut"
In the same way the string you've called "are" will read "arerararraar"
and likewise for the variable you've called "You"
Meanwhile the variable "whatareyou" reads "whatwatwotwutarerararraaryouuyuyuuuu"

In the second section therefore, you're checking whether the person entered "whatwatwotwutarerararraaryouuyuyuuuu", which is of course not very likely!

It would be possible to do this using loops, arrays, and various string functions. However while you're learning the language it is probably best to stick with something simpler first. Be mean for the moment and ignore people who cannot be bothered to spell things correctly. It serves them right after all!
 
Ah I see, that explains a lot actually. Could you give me a short example of how I would achieve this with at least 2 words? Im sure I could tweak it from there and learn what it all means. Or could I not write a list of words in text files e.g. what.txt, are.txt, you.txt and then call the words from there?
 
I maintain that the best way to do this is not to bother. The one simple thing you can do to make things easier for people is to set everything to a single case, so that it catches What Are You, what are you, and WHAT ARE YOU. (The UCASE and LCase commands can do this)
Beyond that, if you're using what amounts to a command line your end user should be willing to learn the commands.
If you mistype anything in CMD it tells you there's a problem, and you have to retype it.


If you really want to go through with this, you'd want an array for each of your words. (Arrays)
Then you'd need to split the user input into individual words searching for the space. (Splitting strings)
Finally you'd need to run through each array with a FOR loop, to check whether the word matches one of the misspellings of the word. You'd also need to record whether any words don't fit.
If all words match one of the spellings you give your message about being the help assistant.
 
I just learned how to use arrays, I have successfully added the list to an array function for each. So I shall read how to split them and hopefully have this done sometime soon, thanks for all the quick replies its much appreciated.
 
Back
Top