Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Working with Classes and Properties for Ga...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Working with Classes and Properties for Game Development in VB.NET
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-07-01

    Table of Contents:
  • Working with Classes and Properties for Game Development in VB.NET
  • Creating an Adventurer Class
  • Setting Properties and Defining Constructors
  • Creating a Tile Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More Visual Basic.NET Articles
    More By Peyton McCullough


       · Hello, all,This is a continuation of my series introducing Visual Basic .NET,...
       · Hi, I think this is just super :) keep up the good work and i will keep on pressing...
     

    VISUAL BASIC.NET ARTICLES

    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT