Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a two classes` Employee and Controller,Employee's fields is` name,surname,sallary.in Controller class I have a Generic array which has 4 Objects, like this`
public List<employee> e = new List<employee>()
{
new Employee("Vazgen","Vazgenyan",3000$),
new Employee("Petros","Petrosyan",3500$),
new Employee("Poghos","Poghosyan",2000$),
new Employee("Karen","Matevosyan",4000$),
};
I must create Function which name is Insert_Data,i must input datas in Console with commas`John,Williams,5000.I must to divide it from commas, the result will be 3 word in array`{ "John", "Williams", "5000"}.With these data I need to create a new Employee and add to my generic array.How can i do it?


Main
Controller c = new Controller();
c.Insert_Data();
when i will call Insert_Data function, it will ask me` Please type new Employee's data
I will write John,Williams,5000 then it will add my new data in Array and will show me,like this`
"Vazgen","Vazgenyan",3000
"Petros","Petrosyan",3500
"Poghos","Poghosyan",2000
"Karen","Matevosyan",4000
"John", "Williams", 5000

What I have tried:

I tried like this`
class Controller
    {
        public List<employee> e = new List<employee>()
        {
            new Employee("Vazgen","Vazgenyan",3000),
            new Employee("Petros","Petrosyan",3500),
            new Employee("Poghos","Poghosyan",2000),
            new Employee("Karen","Matevosyan",4000),
        };
        public void Insert_Data()
        {
            Console.WriteLine("Please type new Emplyoee's data");
            string text = Console.ReadLine();
            string[] arr = text.Split(',');
        }
    }


<pre>class Employee
    {
        public string name;
        public string surname;
        public int sallary;
        public Employee(string a, string b, int c)
        {
            this.name = a;
            this.surname = b;
            this.sallary = c;
        }
    }



<pre>static void Main(string[] args)
        {
            Controller c = new Controller();
            int x;
            while (true)
            {
                Console.WriteLine("\nBelow are list of commands, type one of them to start working.");
                Console.WriteLine("\n1.Insert\n2.Delete\n3.List\n4.Help");
                Console.Write("\nWhat do you want to do: ");
                x = int.Parse(Console.ReadLine());
                Console.WriteLine();
                switch (x)
                {
                    case 1:
                        c.Insert_Data
                        break;
                    case 2:
                        c.Delete_Data
                        break;
                    
                }
            }
            

        }
Posted
Updated 5-Feb-18 23:38pm
Comments
F-ES Sitecore 6-Feb-18 5:06am    
We're not here to do your homework for you, if you've been given this task you must have been told about the relevant methods to use.

We don;t do your homework, so you get no code!
But this is trivial:
1) Read the line from the console.
2) Use string.Split to break it in the comma
3) Check there are exactly three parts after the split. If not, complain.
4) Create a Employee instance from the entered data, using new
5) Use the List.Add method to add you new instance to your collection.
6) Print your collection.

Do note that List<T> is not technically an array (it uses an array internally but that doesn't matter for this) - a List is expandable, you can add and remove items from it. An array is not, you cannot change the size of an array once it is created.
 
Share this answer
 
C#
public void Insert_Data()
{
    Console.WriteLine("Please type new Emplyoee's data");
    string text = Console.ReadLine();
    string[] arr = text.Split(',');
}

What happens to arr when this method ends? It just disappears. You need to add the data from this array into your list somewhere.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900