Home and Learn: VB Net Course
This tutorial follows on from the previous part: Creating Objects. If If you haven't yet read the introduction to Classes, here it is: VB .NET Classes and Objects.
A better way to store values into Instance variables is with the use of Properties. Let's see how that's done.
You've already used properties quite a lot. You add them after the name of an object. A TextBox is a good example. It has lots of properties you can use, one of which is the Text Property. You can get the Text or set the Text. You use these same words, Get and Set, when you're creating your own Properties. Let's see how it's done.
Add a new class to your project. Call it PropertiesTest.
What we'll do is set up a property that converts text in a textbox to uppercase letters. Not the most useful of examples, but it will be easy enough to understand, if we keep it simple.
Add the following Instance variable inside of your new class:
Private studentName As String
Now add this line and hit the Enter key on your keyboard:
Public Property ConvertToUpper()
Visual Studio takes you to a new line. Now enter this:
Get
Hit the Enter key on your keyboard again and Visual Studio adds the rest of the property for you:
Public Property ConvertToUpper()
Get
End Get
Set(value)
End Set
End Property
Add the following line of code between Get and End Get:
Return studentName.ToUpper()
The Get part of a property is like a function, in that you need the Return keyword, followed by whatever you want to return from the property. We're returning the studentName Instance variable converted to uppercase.
For the Set part, add this line:
studentName = value
The Set part is like a Sub, in that it doesn't return a value. You're just setting a value for some Instance variable. The Set has a pair of round brackets with the parameter value inside of them. This is just a variable name, though, and you can change it to anything you like.
Your Class should look like this:
Public Class PropertiesTest
Private studentName As String
Public Property ConvertToUpper()
Get
Return studentName.ToUpper()
End Get
Set(value)
studentName = value
End Set
End Property
End Class
So, we're setting a value for studentName, and we're getting a Return
value after we do something.
Now go back to your form. Add a textbox to your form. Keep it on the default name of TextBox1. Add a new button to the form. Double-click the button to create a code stub.
As the code for your button, add the following three lines:
Dim student As New PropertiesTest()
student.ConvertToUpper = TextBox1.Text
TextBox1.Text = student.ConvertToUpper
The first line creates a new object from our new class (Initializing it, as it's called).
The second line is where we use the Set part of our ConvertToUpper property. Notice that the object name (student) and the Property call (ConvertToUpper) are on the left of an equal sign. After the equal sign is where we store something in our property - the Text from TextBox1. That text will end up in the value variable that was between the round brackets of Set.
The third line Gets a value back out. Notice that, just like a function, the Property call goes after an equal sign.
Try it out. Run your form and enter something in the textbox in lowercase. Click your button and the textbox text should get converted to uppercase.
Go back to your button code, and notice that the first use of the student object, the one before the equal sign, has turned a grey color. If you hover your mouse over the student object, you'll see a message appearing saying, Object Initialization can be simplified. To see what Microsoft considers simplified, keep your mouse over the student object name. You'll see a little icon appear with a lightbulb in it. Click the down arrow and select the menu item that says, Object Initialization can be simplified. Your code will turn into this:
Dim student As New PropertiesTest With {
.ConvertToUpper = TextBox1.Text
}
If you think that that's more complicated than what you had before, hit CTRL Z on your keyboard to undo it. But it's just another way to Initialize an object from a class.
The property we've just set up is a Read and Write property. But you can have a property that's write only, and a property that's read only.
To add a Write Only property, you don't need a Get part. As an example, add this code to your PropertiesTest class:
Public WriteOnly Property DisplayGreeting() As String
Set(value As String)
Dim message As String = "Hello, " &
value
MessageBox.Show(message)
End Set
End Property
Notice that first line of the property had the keyword WriteOnly. After the round brackets, it also has the property type - As String. The rest is the same, and we feel sure you can figure out what it does!
Try it out. Code back to your button code. Add this line:
student.DisplayGreeting = TextBox1.Text
We're setting a property, so the object and property name go on the left side of the equal sign.
Run the code. Enter something in the textbox. As well as converting the text to uppercase, you should a greeting in a message box.
As an example of a ReadOnly property, add this code to your PropertiesTest
class:
Public ReadOnly Property WrapUp() As String
Get
Dim wrapMessge As String = "And that's all, folks!"
Return wrapMessge
End Get
End Property
This time, the keyword on the first line is ReadOnly. It's still As String at the end.
Go back you your button code and try it out by adding these two lines of code:
Dim finalMessage As String = student.WrapUp
MessageBox.Show(finalMessage)
Because WrapUp only has a Get part, it's a ReadOnly property. That's why it's on the right side of the equal sign.
Run your code. Enter some text and click your button. The last thing that happens is the message box displaying showing the wrap message.
OK, that's enough about properties. Let's move on. The final part of
this intro to classes is another subject you'll come across quite a lot
- Inheritance.
Learn how to create Properties in a VB .NET Class -->
Back to the VB NET Contents Page
Email us: enquiry at homeandlearn.co.uk