2

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.

Ralph De Guzman
  • 169
  • 3
  • 11

3 Answers3

2

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
dbasnett
  • 10,010
  • 2
  • 22
  • 32
0

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)
Neolisk
  • 23,880
  • 16
  • 72
  • 135
  • well, I get that I catch the string value, but, how can I add it into the `Cards`? – Ralph De Guzman Aug 27 '14 at 02:34
  • @RalphDeGuzman - You haven't shown us how to instantiate a `Card` instance so we don't know how to go from `string` to `Card`. You need to provide more of your code. – Enigmativity Aug 27 '14 at 02:41
  • Oh I see, Actually, I am into another class named `Deck` with a property of Cards as List(Of Card) – Ralph De Guzman Aug 27 '14 at 02:43
  • @RalphDeGuzman - You need to add the @ Enigmativity (no space between) to your reply if it was intended for me. – Enigmativity Aug 27 '14 at 02:54
  • @Enigmativity - ok. thanks. I edited my question and get the type of `Number` property correct. My real question is, how to add each member of the enum into the list of Card named `Cards` – Ralph De Guzman Aug 27 '14 at 02:57
  • @RalphDeGuzman: Please show a data structure you plan to have in the end, also show code for class Card. – Neolisk Aug 27 '14 at 10:58
0

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

Community
  • 1
  • 1
Bjørn-Roger Kringsjå
  • 9,582
  • 6
  • 32
  • 59