.

Create Properties in your Classes

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.
In this example, we'll allow users to set the height and width of an image. These values will come from textboxes on a form. Before you do the following, download the image you will need for this tutorial:
Once you have the images on your hard drive, do the following:

  • Start a new project

  • 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”
You now need to add a new class. So from the menu bar, click Project. From theProject menu, select Add Class. Give it the Name of changeHeightWidth.vb. Click the Add button, and you'll be taken to the coding window for your new Class.
VB now 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".
On a new line, and before End Class, 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.
But your coding window should now look something like this next image:
The code for your Class
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 isByVal 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 IntegerByVal 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.)

0 comments:

Search Here