如何将枚举添加到列表中

时间:2014-08-27 02:28:23

标签: vb.net list class loops enums

我有一个名为Card的类,其属性为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

我有这个卡号的枚举,用作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

现在,我有一个循环将CardNumbers插入Dim Cards As New List(Of Card),但我不知道如何将每个CardNumbers添加到列表中。正在研究一段时间。有人可以帮忙吗?感谢。

更新 我现在有这个代码添加创建类Card的实例,然后添加到名为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

但是,我收到NullReferenceException错误。

3 个答案:

答案 0 :(得分:2)

某些可用于卡片和套牌的课程。

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

答案 1 :(得分:0)

说你有这一行:

Dim n As CardNumbers = 1

然后这会让你" Ace":

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

答案 2 :(得分:0)

您可以通过以下方式创建13张卡片列表:

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

话虽如此,您可能还想在这篇SO帖子中查看我的答案:

Vb conceptual understanding of creating objects within a class