JS not enabled Useful Visual Basic Knowledge - Daily Manchester
  • Useful Visual Basic Knowledge
Useful Visual Basic Knowledge

[responsivevoice_button voice=”UK English Female” buttontext=”Listen to Post”]

I have been using Visual Basic at College for my diploma and I wanted to perfect my programs, due to this I used my own time to learn more about the Visual Basic programming language and wanted to pass on a few things I have learned. I will also try to provide examples (which indentation is removed when I paste it into WordPress and view the HTML) from two fairly large/complex games which seem simple. but in coding is not, which you can see and even test from looking on my Facebook page.

As well as I will also assume you know the fairly basics, which most of students on my course know / have been taught and expand from there.


Now usually you would declare a variable by doing “Dim Name As String” or “Dim Age As Integer”, but normally you put them inside the private procedure (which I will get into), although sometimes you might want to use it in others. So to accomplish this you would use a global variable, which you achieve by declaring it outside of the “Private Sub” but still in the form/class (“Public Class Form1” for example).

Then sometimes you might need to use them across multiple forms, which is where you make them a public variable in a module. To add a module to your project you press Ctrl+Shirt+A to add a new item, then select the module option. Now instead of using the “Dim” declaration, you now use “Public”.

Structures / Classes

With these you can store multiple variables within one, this might be useful if you are setting varibles about one thing, an example being a person, although I have used a structure in my Word Chain game to hold the scores, which you can see below.

“Structure Score
Dim PlayerName As String
Dim OpponentName As String
Dim Score As Integer
End Structure”

With this you can now declare a varible as the structure, so instead of doing “Dim ScoreExamplePlayerName As String” and so on, you can use “Dim ScoreExample As Score”, then read/write each section by doing “ScoreExample.PlayerName” or “ScoreExample.Score”.

Classes are used for a similar function, but you create a new Class file by using the shortcut Shirt+Alt+C, then naming it what you want your custom variable type / class (So I would name it “Score.vb” and made sure it said “Public Class Score” at the top). Then instead of using “Dim PlayerName As String”, I would use “Public Property PlayerName As String”.


You can think of arrays as columns in an Excel document, where multiple values (of the same type) are stored. When you create one you do it as you would a normal variable, but use “()” at the end, so “Dim Capitals() As String” or “Public” instead of Dim, then either the amount you want it to hold then reference to “Capitals(3)” or write them as I have used here: Public Capitals() = {“London”, “Seoul”, “Tokyo”}

Then if you want to refer to a certain index in the array, you would use “Capitals(1)” to return “Seoul” in this example, since computers start indexing/count at 0, instead of 1.

Also there are the Collection types of arrays, which is an unorganised collection of varibles, unlike a normal array which you use “(2)” in it for example. To declare this you would use the “System.Collections.ArrayList()” declaration, an example being “Dim WordsUsed As New System.Collections.ArrayList()”. This is useful due to the things you can do with it, one being ideal for one of the rules of my Word Chain Game (“3. You can not use a word that has been used before.”) and have implemented it by doing the following (as well as “Add” instead of “Contains” for when it moves onto the next round, before the user enters a new word).

“If WordsUsed.Contains(WordInput) Then
Rule3Broken = True
Rule3Broken = False
End If”

2D arrays

Now you can combine structures/classes and arrays to create a two-dimensional array, similar to how a spreadsheet works. To create this you would use something similar to “Dim Scores(5) As Score”, then to refer to a particular part you would use “Scores(2).PlayerName”.

This has been very useful in my Word Chain game since I generate the array and read in scores from a text file then add one more onto it for the finished games score. I read the amount of lines in the file (code below), divide by 3 (since that is the amount of varibles in the Socre Structure) then add one onto that as the number to put into the array (that number being “ToLoad” in my project).

“FileOpen(1, “RecentScores.txt”, OpenMode.Input)
While Not EOF(1)
Count = Count + 1
End While

Then here is the code I wrote to place them into the 2D array:

“Sub LoadScores(ByVal Scores() As Score)
Dim Count As Integer = 1
FileOpen(1, “RecentScores.txt”, OpenMode.Input)
While Not EOF(1)
Scores(Count).PlayerName = LineInput(1)
Scores(Count).OpponentName = LineInput(1)
Scores(Count).Score = LineInput(1)
Count = Count + 1
End While
End Sub”

Also if you were wondering I wrote to it by wrting the following piece of code, which is ran at the end of the game, which hopefully you’ll understand more at the end of the following section:

“Sub WriteToScoresFile(ByVal Scores() As Score, ByRef ToLoad As Integer)
Using writer As System.IO.StreamWriter = New System.IO.StreamWriter(“RecentScores.txt”, True)
End Using
End Sub”


Now as you can see above, I used a custom procuedure, not bein used by an event, like a button click. Methods encompasses both procedures and functions, now a procedure is used to run code that might be repeated in a program, then is put in its own procedure and called (where you sued to have to use “Call LoadScores()”, but now can just use “LoadScores()”). A function is when you want to return something, you can use it as a vaible in the code when called, so in my Word Chain Game I have said:

“If ReturnFirstChar(WordInput) = ReturnLastChar(LastWord) Then
Rule2Broken = False
Rule2Broken = True
End If”

Where “ReturnFirstChar(WordInput)” is:

“Function ReturnFirstChar(ByRef WordInput As String) As Char
Dim Character As Char
Character = WordInput.Substring(0, 1)
Return Character
End Function”

As you can see you need to say “Return” at the end for it to be a Function, but apart from that that is all there is to it, for now. Also you can use “Substring” on strings to return charcters of it, the first number being where it starts (0 being the first character), then the second number being the amount of characters you want (1 being for a singular character).

Passing Through Variables

In both examples you can see I have used “ByRef” and “ByVal”, when you use a method you can say variables in the method you are calling it from, then declare those in order in the methods code itself. So in my “StartGame” procedure in my WordGame, I have a varible which is “WordInput” (which changes each time the player enters a word for the game), then am using a refernce of it to return a character. I am only using “ByRef” since I do not want to alter the “WordInput” in the game, so it has created a duplicate for the function above. Then you would use “ByVal” to write to it, like I have with my 2D array above.

Others Visual Basic Examples

Combo Box: Adding Items

In my Hangman Game players can select from catagories in a combo box, then if they enter a combo box below the word change to fit the catagory, which might seem hard to do, but I achieved it by wrting this piece of code (ONLY A SNIPIT):

“Private Sub cmbCatagory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCatagory.SelectedIndexChanged

Select Case cmbCatagory.Text
Case “Anime”
Dim x As Integer = 0
x = x + 1
Loop Until x = Anime.Length”

Is a multiple of

If you want to test if a number is a multiple of another, then you would use “MOD”, an example being “If NumberOfGoes Mod 2 = 0 Then”, this being used to alternate the players goes, where 2 is the number its is testing the “NumberOfGoes” varible can be mutpled by (evenly).

Leave a Reply

Your email address will not be published. Required fields are marked *