locked
passing an array to be updated RRS feed

  • Question

  • I have a Windows Forms app that runs all the code to deal cards when I click the deal button (by that I mean that all the code is contained within the objectClick method.

    I would like to be able to split the code into several different cs files.

    If I declare a deck[4.14]( 4 suits with 13 cards each)  array in the clickObject  method how do I pass it to a method in a cs file for filling.


    John Scholl

    Friday, March 9, 2018 8:22 PM

Answers

  • Note, however, that it is usually better in a card game to have the cards be a simple array of 52 elements.  The elements can be a simple object with "suit" and "rank".  When dealing, you don't really care that the cards are organized by suit.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by johnscholl Saturday, March 10, 2018 1:48 AM
    Friday, March 9, 2018 8:56 PM

All replies

  • using System;
    
    class App
    {  
        static void fill( ref int[,] cards )
        {
            for( int i = 0; i < cards.GetUpperBound(0); i++ )
                for( int j = 0; j < cards.GetUpperBound(1); j++ )
                    cards[i,j] = i*100 + j;
        }
    
        public static void Main()
        {
            int[,] cards = new int[4,13];
            fill( ref cards );
            Console.WriteLine( cards[2,2] );
        }
    }
    


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, March 9, 2018 8:54 PM
  • Note, however, that it is usually better in a card game to have the cards be a simple array of 52 elements.  The elements can be a simple object with "suit" and "rank".  When dealing, you don't really care that the cards are organized by suit.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    • Marked as answer by johnscholl Saturday, March 10, 2018 1:48 AM
    Friday, March 9, 2018 8:56 PM
  • You should explore how to create classes to organize things and make the important stuff easier to do and be more object-oriented. If you search you will be surprised how much is available on the subject. So what I am saying is that you should create a Card class that is in a separate file without any UI stuff except maybe the filenames of the card images. The following are some links but there are many more to find.



    Sam Hobbs
    SimpleSamples.Info

    Friday, March 9, 2018 10:50 PM