This tutorial shows you how to use a class so that multiple forms can use your methods. VB version.
Using classes in your Windows Applications not only makes it easier to organize our code, but it also makes it easier to maintain our code. With classes, we can store our methods in one place and use them many times on several Windows Forms (or web pages). Furthermore, when it comes to editing our methods, we only need to edit them in one place - the class.
In this tutorial, we will look at how we can create a class, how we can create methods within that class and also how we can make use of the class in a Windows Form by calling its methods.
The first thing we will do is to create a new project in Visual Studio. This example was created with VS .NET 2008, but the principles apply to 2005 as well. Once we have our project open, we will right-click it in Solution Explorer and choose Add > Class (Or Add New Item, Class). You can name it whatever you want, in this example, we left the default - Class1.
We should then be provided with the default code of the class, which should look something like this:
Public Class Class1
End Class |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
Now if we goto the code of our Form1, by right-clicking it and choosing View Code, we will see the following:
Public Class Form1
End Class |
As opposed to the C# version, there is no namespace in VB. We are now going to create three methods in the class that this form will use, so we will add the following controls to our form: Simple Addition - two labels, two textboxes and a button; Fahrenheit to Celsius - one textbox, one button and one label; Celsius to Fahrenheit - one textbox, one button and one label. The labels will be used to display the results of the methods; you can add more labels to give titles if you wish.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Once we have added these controls to our form, we can begin coding. Let's go back to our class and create our methods. They are very simple methods to show how classes work:
Public Class Class1
Public Shared Function AddNumbers(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y End Function Public Shared Function ToCelsius(ByVal f As Integer) As Integer
Return (f - 30) / 2 End Function Public Shared Function ToFahrenheit(ByVal c As Integer) As Integer
Return (c * 2) + 30 End Function End Class |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
In these methods, we are performing simple calculations to provide functionality to the user, which we are doing with a class.
These methods reside in the Class1 we created, which is in a separate file to our Form1 code. To utilize these methods, we will need to reference the Class name in our Form1 code, like so:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & _
Class1.AddNumbers(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text)).ToString() End Sub |
Notice that we are simply referencing the Class name first, and then the method. We can do the same for the other methods, and also add a try and catch to each of them to catch any errors:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Label2.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & _
Class1.AddNumbers(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text)).ToString() Catch
Label2.Text = "Unable to perform calculation, please try again." End Try End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Label4.Text = Class1.ToCelsius(Convert.ToInt16(TextBox3.Text)) & "°C" Catch
Label4.Text = "Unable to perform calculation," & Constants.vbLf & " please try again." End Try End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Label6.Text = Class1.ToFahrenheit(Convert.ToInt16(TextBox4.Text)) & "°F" Catch
Label6.Text = "Unable to perform calculation," & Constants.vbLf & " please try again." End Try End Sub |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
The entire code-behind of Form1 will look something like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Label2.Text = TextBox1.Text & " + " & TextBox2.Text & " = " & _
Class1.AddNumbers(Convert.ToInt16(TextBox1.Text), Convert.ToInt16(TextBox2.Text)).ToString() Catch
Label2.Text = "Unable to perform calculation, please try again." End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Label4.Text = Class1.ToCelsius(Convert.ToInt16(TextBox3.Text)) & "°C" Catch
Label4.Text = "Unable to perform calculation," & Constants.vbLf & " please try again." End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Label6.Text = Class1.ToFahrenheit(Convert.ToInt16(TextBox4.Text)) & "°F" Catch
Label6.Text = "Unable to perform calculation," & Constants.vbLf & " please try again." End Try End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more .NET Tutorials? Click Here!