Question

sorry if this is erratic as I am super new to programming. I have been searching for help on this but I am unsure what exactly I am missing and looking for.

I have edited this to try and explain the end goal.

The player has two armies, each army has five squads and each squad has five soldiers. Each soldier has a name, level, health and xp which needs to be loaded from the database and into the correct class.

Currently I have it setup as nested classes Army#.Squad#.Soldier#.Property

From the database I can call cells that hold which army > squad > soldier the database row relates to.

What I am trying to do is populate from the database with a loop because there are hundreds of possible combinations.

V1 = Cell1
V2 = Cell2
V2 = Cell3
V4 = Cell4
V1.V2.V3.Health = V4
Was it helpful?

Solution

Indexed properties are going to be the closest to what you want.

Class Army
    Private _Squads(5) As Squad

    Property Squad( ByVal index as Integer ) As Squad
        Get
            Return _Squads( index )
        End Get

        Set( Byval this as Squad )
            _Squads( index ) = this
        End Set
    End Property

    'More
End Class

Class Squad
    Private _Units( 5 ) As Unit

    Property Unit( ByVal index as Integer ) As Unit
        'Same, but use _Units
    End Property
End Class

Class Unit

End Class

Which you could then do the following

Dim x As New Army
x.Squads( 1 ) = New Squad( )
x.Squads( 1 ).Units( 1 ) = New Unit

Unit could also be an Interface, or could just be a class that all of your units inherit from if you have things like swordsman, dragon, soldier, robot, or other unit types.

I haven't tested it, but it should give you an idea of one approach.

OTHER TIPS

I think you are confusing instances and classes. You probably do not need 5 Squad Classes, but likely just need 5 Instances. Consider:

Friend Class Army
     ' an army is made up of 5 squads, so we need 
     ' 5 runtime instances of the Squad Class (lists are like arrays)
     Private squadList As New List(of Squad)

     ' identifier is something the code uses to 
     ' know which to add.  Maybe it is the SquadID from teh DB or name
     ' hard to tell
     Public Sub AddSquad(Identifier As Integer)
         ' create a new Squad instance
         Dim sq As New Squad(maybe some ID)
         ' add new squad to the LIST we are using   
         squadList.Add(sq)
     End Sub        

     Public Sub RemoveSquad(name As String)
        ' a squad died in battle; find them and remove them

        squadList.Remove(indexOfSquadnamed)
        ' now there are only 4
     End Sub

End Class

The "smarts" to construct Squad would be in the Squad Class. You might have one method to load the squad from the DB based on the ID, another to save the squad to the DB at the end of a turn etc. You'd call this in a loop from Army so it saves or loads each squad it currently has.

Even if SquadB is different from SquadA, that can probably be handled by properties and/or as a result of the List(of Soldier) of which it is comprised.

This is all an abstract example. Army would probably never remove a Squad from the List, but mark it as Active = False so that at the end of the turn, dead ones are removed, new ones are added.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top