andrew909
andrew909

Reputation: 15

how to search for a value from an array

I am making a blackjack game using console application and I started to make my card class but I am not sure on how to use my value set accessor to search for value using the array and then determine if the value argument is valid.

Any help would be appreciated

Here is the table I need to use

String[] values = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

Here is my card class so far

class Card
{

    public enum SUIT { HEARTS, SPADES, DIAMONDS, CLUBS };

    private SUIT _suit;
    private Int32 _value;

    public Card(SUIT suit, Int32 value)
    {

        Suit = _suit;
        Value = _value;
    }

    public SUIT Suit
    {
        get
        {
            //Return member variable value
            return _suit;
        }
        set
        {

        }
    }
    //Public FirstName accessor: Pascal casing
    public Int32 Value 
    {
        get
        {
            //Return member variable value
            return _value;
        }
        set
        {

        }
    }
}

Upvotes: 0

Views: 80

Answers (3)

rodrigogq
rodrigogq

Reputation: 1953

Try changing your _value variable to string. Also, you have to use the keyword value in your set statements:

public SUIT Suit
{
    get
    {
        //Return member variable value
        return _suit;
    }
    set
    {
        _suit = value;
    }
}
//Public FirstName accessor
public string Value 
{
    get
    {
        //Return member variable value
        return _value;
    }
    set
    {
         _value = value;
    }
}

If you want to send the index, change the name of your property to Index. Sounds easier! Then use

_value = value;

As Int32. When you wish to retrieve the card, you use:

 = values[Index];

Upvotes: 0

bto.rdz
bto.rdz

Reputation: 6720

If I understand your question you are trying to validate if the value set to your card is in your array. since there are only 13 options, and they will never change, just make an enum as you did with SUIT

public enum CardValue {
One, Two ..... Queen, King
}

public Card(SUIT suit, CardValue value)
{
    Suit = _suit;
    Value = _value;
}

Upvotes: 1

marsh
marsh

Reputation: 2720

If I understand the question try something like:

int index = Array.IndexOf(values, Convert.ToInt32(_value));
if(index > 0)
    _value = value;

Upvotes: 1

Related Questions