Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use return of method in another class in C#

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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++)
        {
            /*/
        }

    }
}

>Solution :

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

int[] array = rnd.generate_random();

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading