Home and Learn: VB Net Course
If you haven't yet read the introduction to Classes, here it is: VB .NET Classes and Objects.
Let's set up a class that we can turn into an object.
Add a new class to your project. Call this one StudentName. As the first two lines of the class, set up some private variables:
Private StudentFamilyName As String
Private StudentPersonalName As String
These two variables are marked private because we don't want anything outside of this class trying to access them. They are both set as strings. We've set them up at the top of the code because we want other Subs and Functions to be able to access them. Variables set up this way are called Instance variables. We didn't have any variables set up this way in our previous static class, which is why we got that strange message about Instance Data. We now have Instance Data (our two variables), so we shouldn't see this message again.
To put something into these two variables, you can add what's called
a Constructor. This Constructor gets called when you turn the class into
an object. It's used to set store values into any Instance variables you
might have set up. (You don't need a Constructor, however, as you can
use Properties to store data in the Instance variables, which we'll do
soon.)
Add this to your new class, just below the two instance variables (they don't need to go below the instance variables, but it looks neater):
Sub New()
StudentFamilyName = "Not Provided"
StudentPersonalName = "Not Provided"
End Sub
A Constructor is set up with the words Sub New. After the word New, you need a pair of round brackets. (You can have more than one Constructor, and you can pass data between the round brackets. But that's slightly too advanced at this stage, so we'll leave it.)
In the code for the Constructor, we're just adding some default values for our two Instance variables.
Now add this Sub, just below the Constructor:
Public Sub DisplayStudentName()
MessageBox.Show(StudentFamilyName & ", " & StudentPersonalName)
End Sub
Notice that there are no dots under the Sub name, this time. That's because we have our Instance Data set up.
To turn this class into an object, go back to your form. Add a new button. Double-click the button to create a code stub. Now add the following line to the button code:
Dim student As New StudentName()
When we set up variables normally, we add As Something on the end, As String, As Integer, As Boolean, etc. You can also add As New YourClassName. Our class name is StudentName. The New keyword means, Create a new object from this class. The object will be stored inside of the name student. Those empty round brackets tell Visual Basic to call that Constructor we set up, the one that stores some default values in our two Instance variables.
Now that we've created a student object from our StudentName class, let's call that Sub into action. Add this line to your button code, just below the first one:
student.DisplayStudentName()
As soon as you type a dot after student, you should see a popup Intellisense box appear. On the list will be the name of that Sub you set up, DisplayStudentName.
Run your code and click your new button. You should see the message box display with the default text, "Not Provided" in it.
Now let's add names to the Sub. In fact, let's overload the Sub.
Go back to your class code. Copy and paste the DisplayStudentName Sub. This time, in between the round brackets of the Sub, add two parameters:
family As String, personal As String
The Sub should look like this:
Public Sub DisplayStudentName(family As String, personal As String)
MessageBox.Show(StudentFamilyName & ", " & StudentPersonalName)
End Sub
Now add these two lines just before the message box:
StudentFamilyName = family
StudentPersonalName = personal
And here's what the overloaded Sub should look like:
Public Sub DisplayStudentName(family As String, personal As String)
StudentFamilyName = family
StudentPersonalName = personal
MessageBox.Show(StudentFamilyName & ", " & StudentPersonalName)
End Sub
Now we're storing data into our two Instance variables. We're storing whatever is inside of family into the StudentFamilyName Instance, and whatever is inside of personal into the StudentPersonalName Instance.
Let's test it out. Go back to your button code. Add this line just below the two you already have:
student.DisplayStudentName("Carney", "Kenny")
Run your code and click your button. You should the first message box
display with its "Not Provided" message. Following that, you
should see a message with a name displayed.
In the next lesson, you'll learn how to create your own Properties in VB NET. Properties are a much better way to hand data to your classes.
Learn about VB NET Properties -->
Back to the VB NET Contents Page
Email us: enquiry at homeandlearn.co.uk