Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
Sign in to follow this  
  • entries
    6
  • comments
    12
  • views
    3,928

Constructing Structures, their uses, and interaction

Sign in to follow this  
formlesstree4

256 views

I do not support the usage, selling, buying, or anything to do with illegal drugs.

Structures aren't exactly the easiest thing for someone to get used to. Their concept is really neat, but an example of actually using one is not all that easy to figure out.

 

The main difference between classes and structures is, classes are reference types and structures are value types. In practical terms, structures are used for smaller lightweight objects that do not persist for long and classes are used for larger objects that are expected to exist in memory for long periods.

 

I was helping a friend the other day with creating a game system (the game's called Drug Wars, a clone of the Linux/DOS version), and realized that a method for storing information wouldn't work with just Lists & Array's alone. I realized that I should be creating a Structure.

 

Here's an example of a Class & Structure side by side:

 

   Public Class NewClass
       Private Var1 As String
       Friend Property Variable1() As String
           Get
               Return Var1
           End Get
           Set(ByVal value As String)
               Var1 = value
           End Set
       End Property

   End Class
   Public Structure NewStruct
       Private Var1 As String
       Friend Property Variable1() As String
           Get
               Return Var1
           End Get
           Set(ByVal value As String)
               Var1 = value
           End Set
       End Property
   End Structure

 

See the difference, or lack thereof? As I said earlier, when one creates a Class, VB.net is expecting the class to persist in memory for quite some time (like the lifetime of the program's execution state), while a Structure is temporary.

 

Take the game I was helping out with, there has to be a way to store all the "Drug" information, and keep it nice and clean.

I could create a "Drug Class", but why? I don't need an entire Class just to do that. I can create a structure, which is basically like creating another object type (Examples of object types: Strings, Integers, My.Setting variables).

 

The Structure would need 3 major items: Name, Cost, Description.

 

    
Public Structure Drug
       Private DName As String
       Private DCost As Double
       Private DDesc As String
       Friend Property Name() As String
           Get
               Return DName
           End Get
           Set(ByVal value As String)
               DName = value
           End Set
       End Property
       Friend Property Cost() As Double
           Get
               Return DCost
           End Get
           Set(ByVal value As Double)
               DCost = value
           End Set
       End Property
       Friend Property Description() As String
           Get
               Return DDesc
           End Get
           Set(ByVal value As String)
               DDesc = value
           End Set
       End Property
   End Structure

 

Now I have a structure that I can easily create many instances of at low memory costs. On top of that, I can even store them inside lists:

 

Dim DrugList As New List(Of Drug)

 

Now I have a fully Object Oriented Structure that I can use (if placed in a publicly available module) anywhere in the program.

 

That being said, what if I created a different structure that required our current one? For example, the Player. The structure would need...what? Name, Money, Inventory List, and a way to add/remove the drugs from his inventory.

 

   
Public Structure Player
       Private PName As String
       Private PMoney As Double
       Private PInventory As List(Of Drug)
       Friend Property Name() As String
           Get
               Return PName
           End Get
           Set(ByVal value As String)
               PName = value
           End Set
       End Property
       Friend Property Money() As Double
           Get
               Return PMoney
           End Get
           Set(ByVal value As Double)
               PMoney = value
           End Set
       End Property
       Friend WriteOnly Property Add_Drug() As Drug
           Set(ByVal value As Drug)
               PInventory.Add(value)
           End Set
       End Property
       Friend Function Remove_Drug(ByVal drug As String) As Boolean
           Dim count As Integer = 0I
           Dim found As Boolean = False
           For Each item In PInventory
               If item.Name = drug Then
                   found = True
                   Exit For
               End If
               count += 1
           Next
           Select Case found
               Case True
                   Money += PInventory(count).Cost
                   PInventory.RemoveAt(count)
               End Select
           Return found
       End Function
   End Structure

 

As you can see, the structure code for the Player is MUCH more in-depth, so let me break this down:

 

There are the regular Properties that you should recognize, such as the ones to Get/Set the Name and Money, but there are some different ones:

There's a WriteOnly Property of adding a Drug. Why not use a sub? There's no real reason I couldn't, it's just a preference really.

 

The hard one was the removing of the drug. Basically, you pass in the name of the drug, the structure loops through the Inventory list (Which is the List(Of Drug)), and when it finds a matching name, presto, it deletes it, credits the money back, and you're good to go.

 

As you can see, these two structures are prime examples (Although the usage of them for the game..I don't necessarily agree with) of how structures can be used and how they can interact with each other seamlessly, and call it's own properties within itself.

 

Feedback is greatly appreciated for my blog posts so I can improve them in the future!

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...