Saturday, 15 September 2012

Creating Properties for your Classes

This tutorial is part of an ongoing lesson. The first part is here: Create your own Classes in VB .NET

In the previous sections, you have learned how to create a Class in VB .NET. You also learned how to create your own Methods. In this part, you'll learn how to create your own properties.

Creating your own Properties in VB .NET Classes

You can add your own Properties to your Class. A Property, remember, is something that changes or sets a value. Examples are, setting the Text in a textbox, changing the background colour of a Form, and setting a Button to be Enabled.

You can Get values from a Property or Set them. So for a Textbox, you can Set the text to appear in the textbox, or you can Get what text is inside of the textbox. You use these same words, Get and Set, when you're creating your own Properties. An example might clear things up. Before you do the following, download the image you will need for this tutorial:

Download the image for these tutorials (WinZip file)

Once you have the images on your hard drive, do the following:

  • Add a Picture Box control to your Form
  • Set the SizeMode Property of the Picture box to StretchImage
  • Click on the Image Property, and add the planet.jpg image that you downloaded above
  • Add two textboxes to the form. Change the Name of the first one to txtHeight, and the second one to txtWidth. Enter 300 as a the text for both textboxes
  • Add two labels to the form. Set the Text of the first one to Height, and the second one to Width. Move them next to the textboxes
  • Add a new button to your form. Set the Text property to “Change Height and Width”

What we’ll do is to give our object the capability of setting a Height and Width property. When the object has done its work, the height and width of the picture box will change to the values from the textboxes. Off we go then.

VB needs to know that you want to set up a Property for your Class. The way you do this is type "Public Property … End Property".

Access the code for your Class. Type a few lines of space between the End Sub of your DoMessageBox Method, and the line that reads "End Class". On a new line, type the following:

Public Property ChangeHeight() As Integer

ChangeHeight is the name of our property, and it's something we made up ourselves. After a pair of round brackets, you add the type of value that will be returned (Just like a function). Here, we want to return an Integer value.

When you press the return key after typing that line, VB finishes off the rest of the code stub for you:

Public Property ChangeHeight() As Integer

Get

End Get

Set(ByVal Value As Integer)

End Set

End Property

Before the code is explained, add a new variable right at the top of your code, just below "Public Class changeHeightWidth". Add this:

Private intHeight As Integer

The Private word means that only code inside of the Class can see this variable. You can't access this code directly from a button on a Form, for example.

The reason the variable is right at the top is so that other chunks of code can see and use it.

 

AddClassProps

With the Get and Set parts, the Property stub is this:

Public Property PropertyName() As VaraibleType

End Property

The reason the Get and Set are there is so that you can Set a value for your property, and get a value back out.

To Set a value, the code inside of Property is this:

Set( ByVal Value As Integer )

End Set

The Set word is followed by a pair of round brackets. Inside of the round brackets is ByVal Value As Integer. The is just like a Sub, when you hand over a value to it. The name of the variable, Value, is a default name. You can change this to anything you like. The type of variable, As Integer, is also a default. You don't have to pass numbers to you property. If you want your Property to handle text you might have something like this:

Set( ByVal MyText As String )

But you couldn't do this:

Set( ByVal Value As Integer, ByVal MyString As String )

In other words, you can't pass two values to your property. You can only pass one value.

But we want to pass a number to our property. For us, this value will come from the textbox on the form. Whatever number is inside of the textbox will get handed over to our Property.

Set( ByVal Value As Integer )

But we need to use this value being handed over. We can assign it to that variable we set up at the top of the Class. So add this to your code (The new line is in bold):

Set(ByVal Value As Integer)

intHeight = Value

End Set

Whenever our Property is called into action, we're setting a Value, and then handing that value to a variable called intHeight. This is known as Writing to a Property.

To read from a Property, you use Get. This will Get a value back out of your Property. The code stub is this:

Get

End Get

You don't need any round brackets for the Get part. You're just fetching something to be read.

Add the line in bold text to your Get statement.

Get

ChangeHeight = intHeight

End Get

All you're doing here is returning a value, just like you do with a function. You're handing a value to whatever name you called your property. We called ours ChangeHeight. It's an Integer. So we can pass whatever value was stored inside of intHeight over to the variable called ChangeHeight:

ChangeHeight = intHeight

You can also use the Return keyword. Like this:

Get

Return intHeight

End Get

Let's see how to use our new Property. (It's not a terribly useful property, by the way. A Picture box already has a Height and Width property that you can use. So ours is a bit redundant. But we're keeping it simple so that you can understand how to create your own properties. And it's not a good idea to put this Property into the code for your ConvertPostcode Class. After all, what has the height and width of a picture box got to do with postcodes? But we've added it here just for convenience sake.)

No comments:

Post a Comment