Click here to Skip to main content
15,890,724 members
Home / Discussions / C#
   

C#

 
GeneralRetrieving the Value of a costumn control placed in a Placeholder placed in a repeater Pin
jawe17-Mar-08 5:15
jawe17-Mar-08 5:15 
GeneralRe: Retrieving the Value of a costumn control placed in a Placeholder placed in a repeater Pin
Not Active17-Mar-08 6:28
mentorNot Active17-Mar-08 6:28 
GeneralMore Collision (XNA) Pin
MasterSharp17-Mar-08 5:01
MasterSharp17-Mar-08 5:01 
GeneralRe: More Collision (XNA) Pin
Christian Graus17-Mar-08 9:15
protectorChristian Graus17-Mar-08 9:15 
GeneralRe: More Collision (XNA) [modified] Pin
MasterSharp17-Mar-08 10:09
MasterSharp17-Mar-08 10:09 
Generalto return or not to return something Pin
pauli17-Mar-08 4:58
pauli17-Mar-08 4:58 
GeneralRe: to return or not to return something Pin
Rob Philpott17-Mar-08 6:20
Rob Philpott17-Mar-08 6:20 
GeneralRe: to return or not to return something Pin
Simon P Stevens17-Mar-08 6:55
Simon P Stevens17-Mar-08 6:55 
Hi pauli, Welcome to code project Smile | :)

To directly answer your question as briefly as possible, I prefer method B. It is a nice clean function call that performs an action and returns the result. Where possible I like to avoid passing objects and modifying them in a function as in a big system you quickly lose track of which objects are being modified where. As far as efficiency is concerned, the difference will be so tiny it's not worth worrying about. Choose readability & maintainability over efficiency in 99.9% of cases.

I do have some other comments though, as you seem to be asking about class design:

You have chosen to store the hands in a multi dimensional integer array.
pauli wrote:
public int[,] PlayerHands = new int[4, 13];

To me, it seems that your combining several bits of information here that should really be separate.
Firstly, rather than an int, why not create a Card class that stores a value and a suit. (The suit member could be an enum). I personally don't like using basic data types (like int) to represent something more complex. In this case, you're using an int from 1-52 to represent a card. Well, when you come back to a bit of code in 6 months, you'll spend ages searching around to figure out what card number 36 was.

public enum CardSuit
{
    clubs = 0,
    hearts = 1,
    spades = 2,
    diamonds = 3
}

public class Card
{
    private int _value;
    private CardSuit _suit;

    public int Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
        }
    }

    public CardSuit Suit
    {
        get
        {
            return _suit;
        }
        set
        {
            _suit = value;
        }
    }
}


Secondly, rather than using a multi dimensional array, (which presents problems if you were to change the number of hands required, or the number of cards in a hand). Why not try defining a class called PlayerHand which would store all the cards for 1 players hand. Inside the PlayerHand class, you could use a Collection<card> member to store the cards in the hand. Collections are easier than arrays to work with, as they can dynamically resize, and include nice add/remove methods. And they are still strongly typed like arrays thanks to generics. Browse around the System.Collections.Generic namespace to see the various available collections. There are different types like lists, dictionaries, etc for different uses.

public class PlayerHand
{
    private Collection<Card> _cards;

    public PlayerHand()
    {
        _cards = new Collection<Card>();
    }

    public Collection<Card> Cards
    {
        get
        {
            return _cards;
        }
    }
}


Finally, now you've got a class for each player hand, you can scrap the Hands class, and just use a collection. (Collection<PlayerHands>);

Now, you could modify your deal method to take in the number of players, and return a collection of the required number of hands:

Collection<PlayerHands> theHands;
theHands = TheDeck.Deal(4);


There's no "correct" way to do anything though, I'm sure other people here will give other opinions, this is just mine.

But back to your original question. I prefer style B. Smile | :)

(It's so nice to have someone ask a well thought out question! Good luck with your programming, feel free to ask more)

Simon

GeneralRe: to return or not to return something [modified] Pin
pauli17-Mar-08 23:01
pauli17-Mar-08 23:01 
GeneralRe: to return or not to return something Pin
Simon P Stevens18-Mar-08 6:07
Simon P Stevens18-Mar-08 6:07 
Generalgeneric collection - .net 2.0 Pin
arkiboys17-Mar-08 4:22
arkiboys17-Mar-08 4:22 
GeneralRe: generic collection - .net 2.0 Pin
Luc Pattyn17-Mar-08 4:29
sitebuilderLuc Pattyn17-Mar-08 4:29 
QuestionActive Directory "login" from C# application Pin
User 137680017-Mar-08 3:52
User 137680017-Mar-08 3:52 
GeneralResource leak (handles count) in simple .NET application Pin
Kolobock17-Mar-08 3:45
Kolobock17-Mar-08 3:45 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Luc Pattyn17-Mar-08 3:55
sitebuilderLuc Pattyn17-Mar-08 3:55 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Kolobock17-Mar-08 4:07
Kolobock17-Mar-08 4:07 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Luc Pattyn17-Mar-08 4:25
sitebuilderLuc Pattyn17-Mar-08 4:25 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Kolobock17-Mar-08 8:32
Kolobock17-Mar-08 8:32 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Luc Pattyn17-Mar-08 8:44
sitebuilderLuc Pattyn17-Mar-08 8:44 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Kolobock17-Mar-08 10:18
Kolobock17-Mar-08 10:18 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Rob Philpott17-Mar-08 4:10
Rob Philpott17-Mar-08 4:10 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Kolobock17-Mar-08 8:34
Kolobock17-Mar-08 8:34 
GeneralRe: Resource leak (handles count) in simple .NET application Pin
Kolobock17-Mar-08 23:57
Kolobock17-Mar-08 23:57 
GeneralIP Camera with C# Pin
sher1217-Mar-08 3:44
sher1217-Mar-08 3:44 
GeneralRe: IP Camera with C# Pin
Luc Pattyn17-Mar-08 3:59
sitebuilderLuc Pattyn17-Mar-08 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.