Working with Classes and Properties for Game Development in VB.NET - Creating an Adventurer Class
(Page 2 of 4 )
Where do we store the name though? We could just create a field to store it in, but there's a better approach. We can create a class to store the name in, along with other data about the player. For now, let's keep the class as simple as possible, but later, we'll make it more complex. In Visual Studio, go to Project on the menu, and click the Add Class option (or, right click the project in the Solution Explorer and, in the context menu, add the class). Let's name the class Adventurer and the class's file Adventurer.vb. The empty class looks like this:
Public Class Adventurer
End Class
In this new class, we need to create a private field for the name, and then we need to create a property to provide access to the field. Creating the field is not very hard. All we need to do is provide an access modifier (Private), an identifier and a type. So, let's create the field first:
Private _name As String
This works just like normal variable declaration, except instead of the Dim keyword, we put an access modifier. We, of course, want our field to be Private, so in order for it to be accessed by another object, we'll need to provide a property. If you come from a language such as Java, this term may sound foreign to you, but it's not too complex. A property merely takes the place of accessor and mutator methods, but it can be accessed similarly to the way a public field is accessed. This way, there are no ugly method calls when the programmer only wants a small piece of data. (Note that any operation that is resource-intensive or takes a while to execute should still be placed in a method rather than a property).
Let's create a property to access the _name field. Here is a bare bones property:
Public Property Name() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
The property is divided into two parts: a Get procedure (the accessor half) and a Set procedure (the mutator half). The Get procedure expects us to return a valid value, or else the code won't compile. So, let's return the value of _name:
Get
Return _name
End Get
Note the use of Return here. Of course, we're free to do any sort of manipulation and computation in the Get procedure, but this is unnecessary for our situation.
The Set procedure doesn't need anything in it for the code to compile, but it would be pointless to leave it blank (you can mark the property as read only by adding the ReadOnly modifier in front of Property, but in this case, you don't need to create Set at all). In this procedure, we need to set _name to whatever value is assigned to the property. The value that is assigned (or, more appropriately, the value that someone has attempted to assign to the property, since we can alter the value or ignore it altogether according to the situation-this is, of course, the beauty and safety of properties) is stored in the value parameter. Let's set _name to whatever is in value:
Set(ByVal value As String)
_name = value
End Set
Next: Setting Properties and Defining Constructors >>
More Visual Basic.NET Articles
More By Peyton McCullough