locked
Class Methods and adding classes into other Classes RRS feed

  • Question

  • So the overview, I'm working on an assignment and I need to add a card to the next available slot in an Array. I have a Two classes, Card and Deck and im trying to make an add method for my Deck class so I can just select a card and a deck and just add it to the right one. 

    So far I have.....

    under my Deck Class

    public int ReturnFirstAvalablePosition()
            {
               int lowest = Array.IndexOf(deck, null);
                return lowest;
            }        

    public void add(Card cardToAdd, Deck deckToAddTo)
            {
                deckToAddTo[ReturnFirstAvalablePosition] = cardToAdd;
            }

    //The return Works for the  first free slot in my array but when I try and make it add to use that to add into a space on a Deck class it just does not work. :( I know I'm probably making a silly mistake.


    Monday, September 26, 2016 10:30 AM

Answers

  • The way you describe the problem, it appears that Array.IndexOf is not able to locate the first null in the array, although you should use the debugger and run the code step-by-step until you find if this is indeed the problem.

    If you finally determine that IndexOf is not working with null, the thing that you should keep in mind is that IndexOf invokes the Equals operator on the objects of the array. Therefore, you should examine such objects (the class Card in this case) and see how its Equals method deals with the nulls.

    Note that you asked the question in the Training and Certification forum. This is the place to ask if you need advice about which course will teach you how to solve your problem, but if you want responses about your C# code, the right place to ask is the C# forum:

    https://social.msdn.microsoft.com/Forums/vstudio/en-us/home?forum=csharpgeneral

    Thursday, September 29, 2016 6:52 AM