Pages

Monday 29 July 2013

VB 11 Tutorial 12: Getting Started

VB 11 Tutorial 11: Getting Started

Today we will be learning on how to use Enums with Select Cases.



Code:

' Example of Using Classes

Module SubNew
Public Enum InventoryItems
Empty = -1
Sword = 0
Shield = 1
Helm = 2
Chest_Plate = 3
Leg_Plates = 4
Bread = 5
Apple = 6
End Enum 

Public Class Character
Dim cEquipment As New Equipment


End Class

Public Class Equipment
Public InventoryItemsEquip(4) As InventoryItems
Public InventoryInBag(15) As InventoryItems

Sub New

for i = 0 to 4
InventoryItemsEquip(i) = InventoryItems.Empty
next

for i = 0 to 15
InventoryInBag(i) = InventoryItems.Empty
next

End Sub

Public Sub Equip(i As InventoryItems)

Select i
Case InventoryItems.Sword 
UnequipIfNotEmpty(0)
Case InventoryItems.Shield 
UnequipIfNotEmpty(1)
Case InventoryItems.Helm 
UnequipIfNotEmpty(2)
Case InventoryItems.Chest_Plate 
UnequipIfNotEmpty(3)
Case InventoryItems.Leg_Plates  
UnequipIfNotEmpty(4)
Case InventoryItems.Bread 
MsgBox("Unable to Equip a Bread...")
Case InventoryItems.Apple 
MsgBox("Unable to Equip a Apple...")
End Case

End Sub

Public Sub UnequipIfNotEmpty(i As Integer)
If InventoryItemsEquip(i) <> InventoryItems.Empty Then
Unequip(i)
End If
End Sub

Public Sub Unequip(l As Integer)
Dim Removed As Boolean = false

For i = 0 to InventoryInBag.Length - 1
If InventoryInBag(i) = InventoryItems.Empty Then
Removed = true
InventoryInBag(i) = InventoryItemsEquip(l)
InventoryItemsEquip(l) = InventoryItems.Empty
End If
Next

If Removed = false then
 InventoryItemsEquip(l) = -1
End If
End Sub

End Class
End Module

No comments:

Post a Comment