0

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?

5
  • Car createCars( returns a single Car, not an array Commented Apr 8, 2020 at 10:21
  • So How would I make it return an array? Commented Apr 8, 2020 at 10:23
  • 1
    To be honest I'm surprised that it's not throwing an IndexOutOfRangeException. return myCars[amountOfCars];, since it appears you're appear to have an "off by one" error. The last element in the array is myCars[amountOfCars-1]; Commented Apr 8, 2020 at 10:23
  • 3
    @phuzi It would throw that exception, if it compiled.... Commented Apr 8, 2020 at 10:26
  • 3
    @phuzi It's not because that would only happen when running the code, but currently it doesn't even compile because of the type mismatch. Commented Apr 8, 2020 at 10:26

3 Answers 3

5

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;

    }
Sign up to request clarification or add additional context in comments.

Comments

3

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]

2 Comments

Since amountOfCars is the size of the array that does not return anything, it would result in an index out of range exception.
Yes.. Sorry My Bad. I just wanted to say the OP that returning as in allocation will not return all the array. I will update appropriately. Thanks BTW.
0

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);

1 Comment

You should add some text and explain what your answer does and why it answers the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.