Home > OS >  How can i add classes or objects to a list?
How can i add classes or objects to a list?

Time:05-02

Me and a friend are working on a text based game in VB.

Class CyberWare
        Public name, description As String
        Public unlocked As Boolean
        Public level, damage, chargeTimes, healing As Integer
    End Class
Dim obj As New CyberWare
Sub UnlockMantisBlades()
        player.UnlockedCyberWare.Add(obj)
        player.UnlockedCyberWare(0).name = "Mantis blades"
        player.UnlockedCyberWare(0).description = "Arm blades designed with lethality and concealment in mind. As effective as they are flashy."
        player.UnlockedCyberWare(0).damage = 50
End Sub
Public Structure PlayerBlueprint
        Public UnlockedCyberWare As List(Of CyberWare)
        Public hp, maxhp, cp, maxcp As Integer
        Public Sub constructplayer()
            hp = 75
            maxhp = 75
            cp = 50
            maxcp = 50
        End Sub
    End Structure
    Dim player As New PlayerBlueprint
    Sub construct()


        player.constructplayer()
        UnlockMantisBlades()
    End Sub

(construct is called in the main method)

we're encountering this error when adding the object to the list. we have tried adding (CyberWare) enter image description here

please help :c

CodePudding user response:

Public UnlockedCyberWare As List(Of CyberWare)

Should be

Public UnlockedCyberWare As New List(Of CyberWare)

CodePudding user response:

The main issue here is that you have, for no apparent reason, declared PlayerBlueprint as a structure rather than as a class. Structure members cannot be initialised where they are declared. That's the reason that you have that constructplayer method to initialise the Integer fields. If you're going to do it that way, you should be initialising the List field there too:

Public Sub constructplayer()
    UnlockedCyberWare = New List(Of CyberWare)
    hp = 75
    maxhp = 75
    cp = 50
    maxcp = 50
End Sub

The better solution would be to declare PlayerBlueprint as a class. You can then initialise each field where you declare it and get rid of that method. Using proper naming conventions and using properties rather than fields for public members, your code should look like this:

Public Class CyberWare
    Public Property Name As String
    Public Property Description As String
    Public Property IsUnlocked As Boolean
    Public Property Level As Integer
    Public Property Damage As Integer
    Public Property ChargeTimes As Integer
    Public Property Healing As Integer
End Class

Public Class PlayerBlueprint
    Public ReadOnly Property UnlockedCyberWare As New List(Of CyberWare)
    Public Property Hp As Integer = 75
    Public Property MaxHp As Integer = 75
    Public Property Cp As Integer = 50
    Public Property MaxCp As Integer = 50
End Class

and then:

Sub UnlockMantisBlades()
    player.UnlockedCyberWare.Add(New CyberWare With {.Name = "Mantis blades",
                                                     .Description = "Arm blades designed with lethality and concealment in mind. As effective as they are flashy.",
                                                     .Damage = 50})
End Sub
  • Related