John John - 1 month ago 10
C# Question

c# Array not printing out contents

I have an array in which the user will insert a 5 jobs which include description, hours it takes to complete and hourly pay. Apologies for the newbie question as I'm new to this language. Any help would be appreciated.

private static void EnterJobs()
{

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

Console.WriteLine("Job " + i);

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

Console.WriteLine("Enter the amount of hours required to complete the job.");
job.hoursToComplete = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the hourly rate for the job.");
job.hourlyRate = Convert.ToInt32(Console.ReadLine());

jobArray[i] = job;
}


When I try to print out the contents of the array it prints out

DemoJobs.Job
DemoJobs.Job
DemoJobs.Job
DemoJobs.Job
DemoJobs.Job


using this loop

for (int i = 0; i < jobArray.Length; i++)
{
Console.WriteLine(jobArray[i]);
}

Answer

I just jotted this up in Notepad, but in your Job class you should have something like the following for your program to know what to print:

public override string ToString()
{
    return this.Description + ", Hours: " + this.Hours.ToString() + ", Rate: " + this.Rate.ToString(); 
}

Then you'd do

Console.WriteLine(job1.ToString());
Comments