Ralph De Guzman
Ralph De Guzman

Reputation: 209

How to add enum to a list

I have a class named Card with a property of CardNumbers

Private _number As CardNumbers
    Public Property Number() As CardNumbers
        Get
            Return _number
        End Get
        Set(ByVal value As CardNumbers)
            _number = value
        End Set
    End Property

I have this enum of cards numbers that was used as the property of Card.

Enum CardNumbers
    Ace = 1
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13
End Enum

Now, I have a loop to insert CardNumbers into a Dim Cards As New List(Of Card), but I do not know how to add each of the CardNumbers into the List. Been researching awhile. Can anyone help? Thanks.

UPDATE: I have now this code to add create an instance of the class Card and then add to the list of Card called Cards:

Dim c As New Card()
        For Each n As CardNumber.CardNumbers In [Enum].GetValues(GetType(CardNumber.CardNumbers))
            c.Number = n
            Cards.Add(c)
        Next

But then, I get a NullReferenceException error.

Upvotes: 2

Views: 553

Answers (3)

dbasnett
dbasnett

Reputation: 11773

Some classes that can be used for cards and decks.

Public Enum aRank
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13
    Ace = 14
End Enum

Public Enum aSuit
    Clubs
    Diamonds
    Hearts
    Spades
End Enum

Class Card
    Private _rank As aRank
    Private _suit As aSuit

    Public ReadOnly Property Rank As aRank
        Get
            Return Me._rank
        End Get
    End Property

    Public ReadOnly Property Suit As aSuit
        Get
            Return Me._suit
        End Get
    End Property

    Public Sub New(rank As aRank, suit As aSuit)
        Me._rank = rank
        Me._suit = suit
    End Sub
End Class

Class DeckOfCards
    Private _deck As List(Of Card)
    Private Shared _prng As New Random

    Public Sub New()
        Me.Shuffle()
    End Sub

    Public Sub Shuffle()
        Me._deck = New List(Of Card)
        For Each r As aRank In [Enum].GetValues(GetType(aRank))
            For Each s As aSuit In [Enum].GetValues(GetType(aSuit))
                Me._deck.Add(New Card(r, s))
            Next
        Next
    End Sub

    Public Function GetCard() As Card
        If Me.CardsRemaining > 0 Then
            Dim idx As Integer = DeckOfCards._prng.Next(Me._deck.Count)
            Dim rvcard As Card = Me._deck(idx)
            Me._deck.RemoveAt(idx)
            Return rvcard
        Else
            '''TODO
            'code for no more cards error
            Return Nothing
        End If
    End Function

    Public ReadOnly Property CardsRemaining As Integer
        Get
            Return Me._deck.Count
        End Get
    End Property
End Class

Upvotes: 2

You can create a list of 13 cards by doing it like this:

Dim list As New List(Of Card)((From item In [Enum].GetValues(GetType(CardNumbers)) Select New Card With {.Number = CType(item, CardNumbers)}))

With that being said, you might also want to look at my answer in this SO post:

Vb conceptual understanding of creating objects within a class

Upvotes: 0

Victor Zakharov
Victor Zakharov

Reputation: 26414

Say you have this line:

Dim n As CardNumbers = 1

Then this will get you "Ace":

Dim s As String = [Enum].GetName(GetType(CardNumbers), n)

Upvotes: 0

Related Questions