Working with Classes and Properties for Game Development in VB.NET - Setting Properties and Defining Constructors
(Page 3 of 4 )
Now we have a functional object with one field and one property to access that field. We can return to the Game module now, where we've just collected the user's name and stored it in name. From here, we need to create an Adventurer object to store the name. This involves creating the object and then setting the property. Let's create the object first:
Dim player As New Adventurer()
This approach works, but since the object is created locally, only Main will have direct access to player. We can fix this either by passing player as an argument to each method, or we can just create player as a field. The latter approach is a lot simpler, so we'll use it. Erase the line creating player as a local variable and add this line at the module-level, up above Main:
Dim player As Adventurer
Now player is a field. Notice how, unlike before, we use Dim rather than an access modifier. If we don't provide an access modifier, then the default is automatically used. Now, back in Main, where we created player before, we instead need to assign a value to the player field:
player = New Adventurer()
Now we just need to specify the name somehow. There are a few ways we can do this. The first way is to assign it directly to the Name property:
player.Name = name
This approach certainly works, but property assignment is made a lot easier with the With operator, which can assign multiple property values when the object is instantiated. Let's instead assign a value to Name using the With operator:
player = New Adventurer() With {.Name = name}
This approach is much nicer, and it's the preferred method for assigning initial property values. The third way is to assign a value to the underlying field in the constructor. The constructor is, of course, called when the object is instantiated. If we don't specify any constructors, then a default one is created for us. Let's, however, create our own constructor. A constructor is created in the same way as a normal method, except it's called New:
Sub New(ByVal name As String)
_name = name
End Sub
Notice, too, the parameter list. We've never talked about parameters before. For each parameter, we must specify an identifier, a type, and a keyword indicating how the parameter is to be passed. Here, we use the ByVal keyword, which is what Visual Studio puts in by default if we don't specify anything. This keyword indicates that the parameter is passed by value. We could also have it passed by reference using the ByRef keyword.
When we create a constructor, then a default, a parameterless constructor will no longer be created for us. So, if you enter the above constructor, then the previous instantiation of an Adventurer object is invalid since no arguments are passed. We need to modify it if it's going to be valid:
player = New Adventurer(name)
Or, we could leave it the same and create a parameterless constructor, though we would, of course, have to assign a value to the Name property manually, as before:
Sub New()
End Sub
That will work for now. We can add complexity to our class later as necessary.
Next: Creating a Tile Class >>
More Visual Basic.NET Articles
More By Peyton McCullough