henry pf
henry pf

Reputation: 310

Returning array of objects from static function

I am making a basic Visual Studio project. Easiest way to explain is to show the code.

using System;
using System.Collections.Generic;

namespace testing
{
    class Program
    {
        static void Main(string[] args)
        {

            int amountOfCars = getAmountOfCars();
            Car[] myCars = createCars(amountOfCars);

        }

        public static int getAmountOfCars (){
            Console.WriteLine("Amount of Cars to enter");
            int amountOfCars = Convert.ToInt32(Console.ReadLine());
            return amountOfCars;
        }


        public static Car createCars(int amountOfCars)

        {
            Car[] myCars = new Car[amountOfCars];
            for (int i = 0; i < amountOfCars; i++)
            {
                Console.WriteLine("Enter brand");
                string brand = Convert.ToString(Console.ReadLine());

                Console.WriteLine("Enter amount of wheels");
                int amountOfWheels = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter amount of seats");
                int amountOfSeats = Convert.ToInt32(Console.ReadLine());

                myCars[i] = new Car(brand, amountOfWheels, amountOfSeats);
            }
            return myCars[amountOfCars];

        }
    }
}

This line

 Car[] myCars = createCars(amountOfCars);

Throws the following error:

Cannot implicitly convert type testing.Car to testing.Car[]

So, I then tried this to convert over

 Car[] myCars = (Car[]) createCars(amountOfCars);

But it still throws the error.

Essentially I am just trying to return the array of objects from createcar function, so that it can be used within the rest of the code.

What is the best practice to solve this?

Upvotes: 0

Views: 1054

Answers (3)

Christian Garcia
Christian Garcia

Reputation: 1

funcion

public static int[] addFirst(int value, int count, int[] test) 
{
    test = test.Where(val => val != value).ToArray();
    Array.Resize(ref test, test.Length + 1);

        for (int i = count-1; i > 0; i--)
        {
            test[i] = test[i-1];
        }
         test[0] = value;
    
    return test;
}

declaracion

array= addFirst(value, array.Length, array);

Upvotes: 0

Sathish Guru V
Sathish Guru V

Reputation: 1479

The signature of the function is

public static Car createCars(int amountOfCars)

instead of

public static Car[] createCars(int amountOfCars)

Also return just the array

return myCars;

instead of

return myCars[amountOfCars]; // This returns only one object at the amountOfCars index in the myCars array. 

Also, this will trigger the ArrayIndexOutOfBoundsException as the myCars is allocated for amountOfCars and array spans from myCars[0] to myCars[amountOfCars-1]

Upvotes: 3

Lennart
Lennart

Reputation: 10322

You need to return an array from createCars():

public static Car[] createCars(int amountOfCars)

    {
        Car[] myCars = new Car[amountOfCars];
        for (int i = 0; i < amountOfCars; i++)
        {
            Console.WriteLine("Enter brand");
            string brand = Convert.ToString(Console.ReadLine());

            Console.WriteLine("Enter amount of wheels");
            int amountOfWheels = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter amount of seats");
            int amountOfSeats = Convert.ToInt32(Console.ReadLine());

            myCars[i] = new Car(brand, amountOfWheels, amountOfSeats);
        }
        return myCars;

    }

Upvotes: 5

Related Questions