2

I've just started learning about methods and classes, I would like to know if I can have something like,

CarsSold Day1 = new CarsSold();

in a for loop where it will create a new instance of a new day each time it runs. For example on the second time the loop runs I want it to create an instance like this,

CarsSold Day2 = new CarsSold(); 

I have tried to use an array but either it cant be done with arrays or I'm using the wrong syntax. Thanks in advance.

Full code

    class Program
{
    static void Main(string[] args)
    {
        int[] weekDay = new int[7];
        int userInput;

        int x;
        for (x = 0; x < weekDay.Length; x++)
        {

            Console.Write("Enter the number of cars sold: ");
            bool ifInt = int.TryParse(Console.ReadLine(), out userInput);

            CarsSold Day[x] = new CarsSold(userInput);
        }
    }
}
12
  • 1
    Can you show how you tried to use arrays? Commented Nov 7, 2018 at 6:57
  • please show what you have tried so far Commented Nov 7, 2018 at 6:57
  • Use a list and then a for loop to add objects every loop into the list. Commented Nov 7, 2018 at 6:58
  • I tried creating an array for day. ( int[] day = new int[7]; ) so that every value in the array is equal to a new instance of the class. Commented Nov 7, 2018 at 6:59
  • 1
    Thanks for all the help guys, this forum is amazing. Commented Nov 7, 2018 at 7:09

3 Answers 3

3

The problem is how you're trying to define your array. The syntax is invalid, and you're doing it in the wrong place.

You should define the array before your loop, and then only assign values to the array within the loop.

static void Main(string[] args)
{
    int userInput;
    CarsSold[] days = new CarsSold[7]; // create an array with 7 items

    int x;
    for (x = 0; x < days.Length; x++)
    {

        Console.Write("Enter the number of cars sold: ");
        bool ifInt = int.TryParse(Console.ReadLine(), out userInput);

        days[x] = new CarsSold(userInput); // assign a value to the days array at position x.
    }
}

Note also that arrays start from 0, not 1, so the first item is actually days[0], and the last is days[6] (for a 7-item array). I took the liberty of removing weekDays since it wasn't needed here, and replaced it in the loop with days.Length.

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

1 Comment

Thanks this solved it, ill select it as the solution when the 3 mins are up. Thanks again!
3

Arrays can have set amount of things in them, so if you declare an array like this

object[] objectArray = new object[10];

Then that array can hold only 10 objects. If you want to add anything to an array you have to chose an index to which that thing will be assigned to, for example:

objectArray[2] = "some value";

in Your case you could iterate through the array and add new object to each index like this

for (var i = 0; i < objectArray.Length; i++)
{
        objectArray[i] = new YourObject();        
}

If the amount of objects you want to store is unknown and can change then you should use collections, for example a List

List<object> listOfObjects = new List<object>();

Now if you want to add anything to that list you simply do

listOfObjects.Add(itemYouWantToAddToTheList);

You access lists the same way you would access arrays, so you can use indexes like

var someValue = listOfObjects[0];

As you probably noticed this list has a generic parameter <object>, it tells the list what type of data it can store, in my example its the object type so it can pretty much store anything, but you can set it to string or int or any other type like your own class types and then this list would store only those types of objects.

1 Comment

Thank you!, I've never used a list before, ill defiantly look into trying this.
1

If you don't know the number of days, then:

IList<CarsSold> soldCarsList = new List<CarsSold>();

foreach(var day in Days)
{
    soldCarsList.Add(new CarsSold());
}

If you know the number of days(e.g:7), then:

CarsSold[] soldCarsArray = new CarsSold[7];

for (int i = 0; i < days.Length; x++)
{
   soldCarsArray[i] = new CarsSold();
}

Comments

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.