简体   繁体   中英

C# Array of instance of Class

Here I'm trying to get the data for many employees that i predefined a class for it as This

public class Employee
{
    public int code;
    public float salary;
    public float bonus;
    public float deduction;
}

and i'm trying to make a function to create the array for the Employees and to ask the user to fill it

That's the code for now

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task3
{
    class Program
    {
        static void Main(string[] args)
        {
            int size = 3;
            Employee[] E1 = GetEmployeeData(size);

        }

        static Colors GetIntFromUser(string Message)
        {
            int result;
            Console.Write(Message);
            result = int.Parse(Console.ReadLine());
            Colors c = (Colors)result;
            return c;
        }
        static Employee[] GetEmployeeData(int size)
        {
            for (int i = 0; i < size; i++)
            {
                Employee[] E = new Employee [size];
                E[i] = new Employee();
                Console.Write("Code: ");
                E[i].code = int.Parse(Console.ReadLine());
                Console.Write("Salary: ");
                E[i].salary = float.Parse(Console.ReadLine());
                Console.Write("Bonus: ");
                E[i].bonus = float.Parse(Console.ReadLine());
                Console.Write("Deduction: ");
                E[i].deduction = float.Parse(Console.ReadLine());
                return E[i];
            }
        }
    }
}

i get that Error Severity Code Description Project File Line Suppression State Error CS0161 'Program.GetEmployeeData(int)': not all code paths return a value Task3 E:\\ITI39\\Intro to programming\\tasks\\day5\\ConsoleApplication1\\Task3\\Program.cs 26 Active

and This one too Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'Task3.Employee' to 'Task3.Employee[]' Task3 E:\\ITI39\\Intro to programming\\tasks\\day5\\ConsoleApplication1\\Task3\\Program.cs 40 Active

PS I don't Have Any OOP Experience more further i'm beginner in Programming

The first error you are seeing is due to you only returning a value inside of the loop - if size were 0 then the loop would not execute and therefore no value would be returned. The second error is due to you trying to return a single part of the array at the end of each loop. You need to return the whole array after the loop completes so rather than write

return E[i];

you should write

return E;

And this should be written after the loop ends, once you have received all 3 entries.

However there are other issues here as you create the 'E' variable each time the loop iterates and so you need to move the declaration of 'E' to be before the loop starts otherwise the result will only ever contain a valid object at index 2. The entire corrected function would look like this:

static Employee[] GetEmployeeData(int size)
{
    Employee[] E = new Employee [size];
    for (int i = 0; i < size; i++)
    {
        E[i] = new Employee();
        Console.Write("Code: ");
        E[i].code = int.Parse(Console.ReadLine());
        Console.Write("Salary: ");
        E[i].salary = float.Parse(Console.ReadLine());
        Console.Write("Bonus: ");
        E[i].bonus = float.Parse(Console.ReadLine());
        Console.Write("Deduction: ");
        E[i].deduction = float.Parse(Console.ReadLine());
    }

    return E;
}

You may also want to consider what would happen if the user was to enter a value that cannot be parsed to the type expected and defensively code for this - take a look at the TryParse function instead.

You could use an Iterator and yield return the employees. This enables you to stream the results.

static IEnumerable<Employee> GetEmployeeData(int size)
{
    for (int i = 0; i < size; i++)
    {
        Employee e = new Employee();
        Console.Write("Code: ");
        e.code = int.Parse(Console.ReadLine());
        Console.Write("Salary: ");
        e.salary = float.Parse(Console.ReadLine());
        Console.Write("Bonus: ");
        e.bonus = float.Parse(Console.ReadLine());
        Console.Write("Deduction: ");
        e.deduction = float.Parse(Console.ReadLine());
        yield return e;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM