Guide to VB.Net: Advanced
Guide to VB.Net: Advanced
Viewing this guide means that you have viewed the intermediate tutorial. If you have not, go take a look at it. In this tutorial, you will learn how to extend upon what you already know to the point where it can be practically used in a useful application.
Part 1: Operators
Now, you probably already have recognized the standard operator of addition (+) but what are all the others? These operators will be essential to number based functions which you will need to create useful programs. The most common operators are listed below:
There are also inequality operators that we will need to know, these will be essential as well. Below are the most common inequality operators:
Part 2: Do Loop
I briefly explained the Do/Loop function in the last tutorial. Whatever is contained within the first line Do, and the final line Loop, will be repeated forever but sometimes forever isn't best. In order to Loop until a specific criteria is met, we can use the While or Until methods. The code below should explain:
Part 3: Accessing Data
In many cases, you will be in a situation where you will have multiple forms and you will need to access the data or even share the data amongst the forms. This can be done in different ways which are very simple.
The easy way into doing this is simply by adding a small piece of text to your code. Let's say you want to change the textbox in form2's text to something else. In order to do this, we could simple access it as shown below:
Part 4: Imports
For some odd reason, Microsoft thought it was a good idea to not automatically include everything in your project, so you have to Imports things to accomplish certain tasks. This is very simple, all you need to put is Imports and a Namespace to import into the project. Shown below is importing the Threading namespace. Without this threading imported, the Sleep code would not work and would result in an error.
It's just that simple, for programmers at this level, you will basically only need System.IO, System.Threading, and possibly System.Text if you're that good.
Part 5: e as Arguments
In most subs, there is all that crap you need in order to handle a function such as Button1 being clicked. If you look closely, e is always the constant declaration in them but what does it do? Lets test it out with a Web Browser control.
Part 6: Lesson Test
Now, your lesson test for this will be to put together an application that uses a lot of numerical functions. Make a Do/Loop that will increase the integer declared by any amount each time, and every time it's looped, make it complete a formula. Create a messagebox of the result of that formula and have the thread sleep for 2 seconds until the loop. Loop 5 times and then stop.
◄ Previous
Viewing this guide means that you have viewed the intermediate tutorial. If you have not, go take a look at it. In this tutorial, you will learn how to extend upon what you already know to the point where it can be practically used in a useful application.
Part 1: Operators
Now, you probably already have recognized the standard operator of addition (+) but what are all the others? These operators will be essential to number based functions which you will need to create useful programs. The most common operators are listed below:
- + Addition
- - Subtraction
- * Multiplication
- / Division
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intDegreesF As Integer = 70
Dim intDegreesC As Integer
intDegreesC = (9 / 5) * intDegreesF + 32
End Sub
End ClassThis simple code converts Fahrenheit into Celsius.There are also inequality operators that we will need to know, these will be essential as well. Below are the most common inequality operators:
- = Equals
- <> Not Equal
- < Less Than
- > Greater Than
- >= Greater Than or Equal To
- <= Less Than or Equal To
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 3
If i > 5 Then
MsgBox("I is greater than 5")
ElseIf i <= 2 Then
MsgBox("I is less than (or is) 2")
ElseIf i = 6 Then
MsgBox("I is 6")
End If
End Sub
End ClassPart 2: Do Loop
I briefly explained the Do/Loop function in the last tutorial. Whatever is contained within the first line Do, and the final line Loop, will be repeated forever but sometimes forever isn't best. In order to Loop until a specific criteria is met, we can use the While or Until methods. The code below should explain:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
Do While i <= 10
MsgBox(i)
i = i + 1
Loop
End Sub
End ClassThis will only execute the commands while i is less than or equal to 10. If i is greater than 10 then it will continue the sub beyond the Loop. This code can also be replicated using the Do Until (my preference) method as shown below:Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 0
Do Until i = 11
MsgBox(i)
i = i + 1
Loop
End Sub
End ClassIn this code, once i is equal to 11, it will continue the sub beyond the Loop.Part 3: Accessing Data
In many cases, you will be in a situation where you will have multiple forms and you will need to access the data or even share the data amongst the forms. This can be done in different ways which are very simple.
The easy way into doing this is simply by adding a small piece of text to your code. Let's say you want to change the textbox in form2's text to something else. In order to do this, we could simple access it as shown below:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CString As String = "String Changed"
Form2.Textbox1.Text = CString
End Sub
End ClassSee? The only difference was adding Form2. in front of the textbox's name. You can read and write data to external forms easily like this. Now, the more pro way to go about doing this is by using a module. To get a module, go to Project>Add Windows Form>Module and a new module will be created. The only requirement for a module's data to be accessed is by declaring it using Public. An example is shown below:Module Module1
Public String1 As String
End ModuleNow, in any form, String1 can be referred to without any precedents. For example:Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
String1 = "k"
End Sub
End ClassThe application already knows that we're talking about Module1.String1 since its been declared using Public method.Part 4: Imports
For some odd reason, Microsoft thought it was a good idea to not automatically include everything in your project, so you have to Imports things to accomplish certain tasks. This is very simple, all you need to put is Imports and a Namespace to import into the project. Shown below is importing the Threading namespace. Without this threading imported, the Sleep code would not work and would result in an error.
Imports System.Threading
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.Sleep(5000)
End Sub
End ClassIt's just that simple, for programmers at this level, you will basically only need System.IO, System.Threading, and possibly System.Text if you're that good.
Part 5: e as Arguments
In most subs, there is all that crap you need in order to handle a function such as Button1 being clicked. If you look closely, e is always the constant declaration in them but what does it do? Lets test it out with a Web Browser control.
Public Class Form1
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
MsgBox(e.Url.AbsoluteUri)
End Sub
End ClassAs you can see, e is a variable and has it's own little properties. After pressing the period, the list showed that URL was my only choice, so that's what I entered. Then, there were even more properties of that property and I chose AbsoluteURI. Now, what is returned after this if my browser is set to view Google on form loadup? Copy the code and find out. If you did it correctly, it would write the page title, and the URI. If you simply explore all of the declarations' arguments, you can find that it will be very useful in all your applications.Part 6: Lesson Test
Now, your lesson test for this will be to put together an application that uses a lot of numerical functions. Make a Do/Loop that will increase the integer declared by any amount each time, and every time it's looped, make it complete a formula. Create a messagebox of the result of that formula and have the thread sleep for 2 seconds until the loop. Loop 5 times and then stop.
◄ Previous