Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 2
    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 - 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.

    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

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT