• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C# gurus, enlighten me

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
I actually wanted to make the whole Deck static as I only want there to be 1 deck during the entire execution. But when I declared the class static it gave me problems. I might be able to fix it though.

singleton pattern is what you are looking for.


Code:
class Dao 
{

        private static Dao _dao;

        private Dao()
        {
        }

        public static Dao GetInstance()
        {
            if (_dao != null) return _dao;
            _dao = new Dao();
            return _dao;
        }
}

this is a straight copypasta from a project I'm working on (Data access object class), and this ensures that you will ever only have one instance of that class. it's probably the most basic design pattern out there, but I've found it to be the one I use the most.
 
It would be rather poor design to use a singleton for something like a card deck. There are plenty of games where you do want multiple decks. If you're lazy now and use a singleton, then you create a ton of refactoring work for yourself if you later decide to implement something that needs more than one deck.

Mark_R is correct, just make a normal class and only create one instance of it.
 
Singleton wouldn't be too bad, but it is incredibly overused.

Just create an instance of your deck in void Main or wherever, I doubt it will be too hard to keep track of it.

If at all possible, I wouldn't even bother to create a deck class. The simplest possible deck instance:

public static var Deck = new List<Card>();

But I guess you'd probably want to encapsulate a Shuffle() method, maybe a GetNextCard() method, along with the deck itself, so a deck class might be better.
 
Yeah guys I was just playing with it that's all. Rather use it in an overly simple problem than try to figure it out when it matters 😉
 
singleton pattern is what you are looking for.


Code:
class Dao 
{

        private static Dao _dao;

        private Dao()
        {
        }

        public static Dao GetInstance()
        {
            if (_dao != null) return _dao;
            _dao = new Dao();
            return _dao;
        }
}

this is a straight copypasta from a project I'm working on (Data access object class), and this ensures that you will ever only have one instance of that class. it's probably the most basic design pattern out there, but I've found it to be the one I use the most.

Nice... Makes sense to me!
 
Singleton wouldn't be too bad, but it is incredibly overused.

Just create an instance of your deck in void Main or wherever, I doubt it will be too hard to keep track of it.

If at all possible, I wouldn't even bother to create a deck class. The simplest possible deck instance:

public static var Deck = new List<Card>();

But I guess you'd probably want to encapsulate a Shuffle() method, maybe a GetNextCard() method, along with the deck itself, so a deck class might be better.

Yeah that's what I was thinking... shuffle & deal.
 
Back
Top