Home > Software design >  Use return of method in another class in C#
Use return of method in another class in C#

Time:06-09

First of all, I'm new in C# (and in development overall). My question is quiet simple.

I've two different classes. A method of one of my class is returning an array. I'm trying to use this array in a method of the other class, here is the code:

The idea here is to retrieve those numbers stored in the array, and generate cards with related ID (stored in DB) in the "for" statement.

Many tanks in advance!

class c_random
{
    int nbr_random_to_generate = 2; // number to generate

    /*
     * This function 
     *  - execute the methods to counts nbr of cards in DB
     *  - Create an Array with id of cards to generate
     *  */
    public int[] generate_random()
    {
        Random rnd = new Random();
        c_ConnectionMySQL obj_sqlRes = new c_ConnectionMySQL(); // Create a connection to MySQL
        obj_sqlRes.count_cards_DB(); // Execute the method to count the number of cards in DB
        Console.WriteLine(obj_sqlRes.nbr_cards);


        int[] array_random = new int[nbr_random_to_generate];
        for (int i = 0; i < nbr_random_to_generate; i  )
        {
            array_random[i] = rnd.Next(obj_sqlRes.nbr_cards); // generate an array of x (nbr_random_to_generate), with the count of cards as limit
        }
        return array_random;
    }
}


class c_Hand
{
    // ATTRIBUTES
    int nbr_in_hand;

    

    // METHODS
    public void HandGeneration(int a)
    {

        int a;
        c_ConnectionMySQL countCards = new c_ConnectionMySQL();
        countCards.count_cards_DB();

        c_card hand = new c_card();
        c_random rnd = new c_random();

        rnd.generate_random();


        for (int i = 0; i < 2; i  )
        {
            /*/
        }

    }
}

CodePudding user response:

You can assign the return of generate_random() to a variable when it is called.

int[] array = rnd.generate_random();

  • Related