Visual Basic Lesson 7:
What's next then? The Mid function of course! It's an essential function which will form the basis for the programming coursework. Why is it the Mid function? Well, because it returns a value back. It's a very useful function, but before you can start programming you need to understand this formula:Mid(string, start, length)
e.g. Dim myString As String
Dim extractedChars as String
myString = InputBox("Enter suitable string.")
extractedChars = Mid(myString, 2, 6)
In this case myString can be "Harvard", therefore the extractedChar variable will take "arvard" from the word.
The "string" in the formula is the string you wish to use the function on.
The "start" is where you'd like the Mid function to start.
The "length" is the number of characters you wish to get.
What that example essentially shows is a program which can take a section of the string and move it over into another variable, which is useful for spellchecks.
When you don't know the length of the word you are wishing to spellcheck you merely use a For Next loop and use the built in .length as shown in this example program I created during class:
The above program uses two Mid functions. One to take the letters from the actual answer "colloportus" and one to take the letters from the student answer. The
If studentChar <> answerChar Then
counter = counter + 1
acts as an error check. It means that the first letter of the student answer and the first letter of the actual answer are compared and if they don't match then the counter goes up. The loop ensures that each letter of the student answer is checked with the actual answer. Later on in the program there are three If Statements which determine if the spellings are minor or major errors.
The reason the Mid function doesn't have to be typed for each letter is because x is defined as a range which uses the .length function to ensure all letters are checked, the loop runs through until each letter is checked.
And that's pretty much the Mid function. It's a lot easier to understand when you see it's application in a program, that than just reading or listening to an explanation.
Until next time :D.
No comments:
Post a Comment