John John - 1 year ago 146
C# Question

C# Populate object array with user input

I'm new to c# and having a hard time figuring out how to populate an array from user input. I have an array of 5 job objects

static Job[] jobArray = new Job[5];

The user will be inputting a description for each job, time to complete each job and the pay for each job. I need to put those inputted values into the array.
Any help would be appreciated, thanks.

Answer Source

Basically what you need to keep in mind is that the row above where you initialize an array does not create the objects within it but only the array.

For each position of the array you need to request the information from the user and store it in the proper property. Then you assign that new object to the array.

This code sample does it for the Description property of Job

Job[] jobArray = new Job[5];

for (int i = 0; i < jobArray.Length; i++)
   Job job = new Job();

   Console.WriteLine("Enter description");
   job.Desciption = Console.ReadLine();

   jobArray[i] = job;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download