Hi there, so im trying to get something done for my class. Im supposed to implement something where you can enter details of a person, validate them, then store them as a array, and allow you to add more later. What I dont understand is how to store a group of variables (ie. Name, phone number, postcode) in one array and when another record is added, simply add it on, without erasing the contents of the previous record.
I hope ive asked this in the correct place. Was just hoping for a bit of help, as all tutorials oh the internet only seem to cover entering pre-made data into arrays.
Thanks

use generics...

List<string> personsData = new List<string>();
string personData = "name,phone,number,postcode";
personsData.Add(personData);
//use System.IO.File class to store personsData content...

Sorry, probably should've mentioned im pretty new at c#. Line 1 declares the array as a string. Line 2 assigns the variables to it. What does line 3 do and how would I allow entryof more then 1 record?

Thanks, ive read through those, paticually the array section many times and still dont understand. Heres a snippet of the code im trying to use at the moment:

using System;

namespace Assignment1
{
    class MainClass
    {
        public static void Main()
        {
            string salary, pno, pcode, state,addr, strno, name;
            Console.WriteLine("enter salary:");
            salary = Console.ReadLine();

            Console.WriteLine("enter phone number: ");
            pno = Console.ReadLine();

            Console.WriteLine("enter post code: ");
            pcode = Console.ReadLine();

            Console.WriteLine("enter state: ");
            state = Console.ReadLine();

            Console.WriteLine("enter adress: ");
            addr = Console.ReadLine();

            Console.WriteLine("enter street number: ");
            strno = Console.ReadLine();

            Console.WriteLine("enter name: ");
            name = Console.ReadLine();

            string[] ArrayRec = {salary, pno, pcode, state, addr, strno, name};
            foreach (string Details in ArrayRec)
            {
                Console.WriteLine("Details: = " + Details);
            }
        }
    }
}

what I dont understand is how I would add a second record onto this. Like add another person to which I could store those details again.
Any ideas?

You should add a new class Person and use a generic list.

Here's an example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            [B]List<Person> people = new List<Person>();[/B]

            string salary, pno, pcode, state, addr, strno, name;

            Console.WriteLine("enter salary:");
            salary = Console.ReadLine();

            Console.WriteLine("enter phone number: ");
            pno = Console.ReadLine();

            Console.WriteLine("enter post code: ");
            pcode = Console.ReadLine();

            Console.WriteLine("enter state: ");
            state = Console.ReadLine();

            Console.WriteLine("enter adress: ");
            addr = Console.ReadLine();

            Console.WriteLine("enter street number: ");
            strno = Console.ReadLine();

            Console.WriteLine("enter name: ");
            name = Console.ReadLine();
            
            // You can add as many people as you want
            people.Add(new Person(name, salary, pno, pcode, state, addr, strno));

            
        }
    }

    class Person
    {
        public string Name, Salary, Pno, PCode, State, Address, StrNo;

        public Person(string name, string salary, string pno, string pcode, string state, string address, string strNo)
        {
            this.Name = name;
            this.Salary = salary;
            this.Pno = pno;
            this.PCode = pcode;
            this.State = state;
            this.Address = address;
            this.StrNo = strNo;
        }

    }
}

Thanks

Thanks faroqa, that helped immensely, heres my code as of now:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{

    class Program
    {

        static void Main(string[] args)
        {
            int select;
            while (select != 3)
            {
                Console.WriteLine("1. Add new");
                Console.WriteLine("2. View records");
                
                switch(select)
                {
                    case 1:
                        List<Person> people = new List<Person>();

                        string salary, pno, pcode, state, addr, strno, name;

                        Console.WriteLine("enter salary:");
                        salary = Console.ReadLine();

                        Console.WriteLine("enter phone number: ");
                        pno = Console.ReadLine();

                        Console.WriteLine("enter post code: ");
                        pcode = Console.ReadLine();

                        Console.WriteLine("enter state: ");
                        state = Console.ReadLine();

                        Console.WriteLine("enter adress: ");
                        addr = Console.ReadLine();

                        Console.WriteLine("enter street number: ");
                        strno = Console.ReadLine();

                        Console.WriteLine("enter name: ");
                        name = Console.ReadLine();

                        // You can add as many people as you want
                        people.Add(new Person(name, salary, pno, pcode, state, addr, strno));
                        break;
                    case 2:
                        for (int i = 0; i < List; i++) [B]//<- ERRORS HERE[/B]
                            Console.WriteLine(List[i]); [B]//<-And here[/B]
                        break;
                    case 3:
                        break;

                }
            }


        }
    }

    class Person
    {
        public string Name, Salary, Pno, PCode, State, Address, StrNo;

        public Person(string name, string salary, string pno, string pcode, string state, string address, string strNo)
        {
            this.Name = name;
            this.Salary = salary;
            this.Pno = pno;
            this.PCode = pcode;
            this.State = state;
            this.Address = address;
            this.StrNo = strNo;
        }

    }
}

The two error messages I get are:
'Using the generic type 'System.Collections.Generic.List<T>' requires '1' type arguments'
Any ideas why this would be? I think after this it should be pretty much smoonth sailing
---
Sorry, to clear it up I want the part of the code that errors, to print the contents of the 'people' array.

Use this code:

for (int i = 0; i < people.Count; i++)
Console.WriteLine(people[i]);

Thanks

Hi there, heres my coding as of now:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{

    class Program
    {

        static void Main(string[] args)
        {
            int select = 0;
            List<Person> people = new List<Person>();
            while (select != 3)
            {
                Console.WriteLine("1. Add new");
                Console.WriteLine("2. View records");
                select = int.Parse(Console.ReadLine());
                
                switch(select)
                {
                    case 1:
                         

                        string salary, pno, pcode, state, addr, strno, name;

                        Console.WriteLine("enter salary:");
                        salary = Console.ReadLine();

                        Console.WriteLine("enter phone number: ");
                        pno = Console.ReadLine();

                        Console.WriteLine("enter post code: ");
                        pcode = Console.ReadLine();

                        Console.WriteLine("enter state: ");
                        state = Console.ReadLine();

                        Console.WriteLine("enter adress: ");
                        addr = Console.ReadLine();

                        Console.WriteLine("enter street number: ");
                        strno = Console.ReadLine();

                        Console.WriteLine("enter name: ");
                        name = Console.ReadLine();

                        // You can add as many people as you want
                        people.Add(new Person(name, salary, pno, pcode, state, addr, strno));
                        break;
                    case 2:
                        for (int i = 0; i < people.Count; i++)
                            Console.WriteLine(people[i]);
                        break;
                    case 3:
                        break;

                }
            }


        }
    }

    class Person
    {
        public string Name, Salary, Pno, PCode, State, Address, StrNo;

        public Person(string name, string salary, string pno, string pcode, string state, string address, string strNo)
        {
            this.Name = name;
            this.Salary = salary;
            this.Pno = pno;
            this.PCode = pcode;
            this.State = state;
            this.Address = address;
            this.StrNo = strNo;
        }

    }
}

The program runs fine but when after I enter a record and request to display it, for each record it only displays 'ConsoleApplication.Person'
:/ ideas? thanks again

>The program runs fine but when after I enter a record and request to display it, for each record it only displays 'ConsoleApplication.Person'

for (int i = 0; i < people.Count; i++)
   Console.WriteLine(people[i].Name +  "  " + people[i].Salary);
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{

    class Program
    {

        static void Main(string[] args)
        {
            int select = 0;
            //List<Person> people = new List<Person>();
            string[] people = new string[50];
            while (select != 3)
            {
                Console.WriteLine("1. Add new");
                Console.WriteLine("2. View records");
                select = int.Parse(Console.ReadLine());
                
                switch(select)
                {
                    case 1:
                         

                        string salary, pno, pcode, state, addr, strno, name;

                        Console.WriteLine("enter salary:");
                        salary = Console.ReadLine();

                        Console.WriteLine("enter phone number: ");
                        pno = Console.ReadLine();

                        Console.WriteLine("enter post code: ");
                        pcode = Console.ReadLine();

                        Console.WriteLine("enter state: ");
                        state = Console.ReadLine();

                        Console.WriteLine("enter adress: ");
                        addr = Console.ReadLine();

                        Console.WriteLine("enter street number: ");
                        strno = Console.ReadLine();

                        Console.WriteLine("enter name: ");
                        name = Console.ReadLine();

                        // You can add as many people as you want
                        new Person(name, salary, pno, pcode, state, addr, strno);
                        break;
                    case 2:
                        for (int i = 0; i < people.Length; i++)
                            Console.WriteLine(people[i].Name + "  " + people[i].Salary);
                            
                        break;
                    case 3:
                        break;

                }
            }


        }
    }

    class Person
    {
        public string Name, Salary, Pno, PCode, State, Address, StrNo;

        public Person(string name, string salary, string pno, string pcode, string state, string address, string strNo)
        {
            this.Name = name;
            this.Salary = salary;
            this.Pno = pno;
            this.PCode = pcode;
            this.State = state;
            this.Address = address;
            this.StrNo = strNo;
        }

    }
}

---
Sorry, I didnt see you had reposted. I converted all the code you supplied me with to arrays rather then lists, as that is what we are supposed to be using.
Now it errors as:
'string' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
'string' does not contain a definition for 'Salary' and no extension method 'Salary' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

commented: Purchase some good books and learn c#!!! -2

Tip: Don't use an array. It is rare, in reality, you want to use an array. Definitely use a List<T> any time you want to add/delete elements, since resizing arrays is expensive.

static void Main(string[] args)
        {
            int select = 0;

            List<string[]> people = new List<string[]>();
            while (select != 3)
            {
                Console.WriteLine("1. Add new");
                Console.WriteLine("2. View records");
                select = int.Parse(Console.ReadLine());

                switch (select)
                {
                    case 1:


                        string salary, pno, pcode, state, addr, strno, name;

                        Console.WriteLine("enter salary:");
                        salary = Console.ReadLine();

                        Console.WriteLine("enter phone number: ");
                        pno = Console.ReadLine();

                        Console.WriteLine("enter post code: ");
                        pcode = Console.ReadLine();

                        Console.WriteLine("enter state: ");
                        state = Console.ReadLine();

                        Console.WriteLine("enter adress: ");
                        addr = Console.ReadLine();

                        Console.WriteLine("enter street number: ");
                        strno = Console.ReadLine();

                        Console.WriteLine("enter name: ");
                        name = Console.ReadLine();

                        // You can add as many people as you want

                        people.Add(new string[] { name, salary, pno, pcode, state, addr, strno });
                        break;
                    case 2:
                        for (int i = 0; i < people.Count; i++)
                        {
                            Console.WriteLine(people[i][0] + "  " + people[i][1]);
                        }

                        break;
                    case 3:
                        break;

                }
            }


        }

Tip: Don't use an array. It is rare, in reality, you want to use an array. Definitely use a List<T> any time you want to add/delete elements, since resizing arrays is expensive.

static void Main(string[] args)
        {
            int select = 0;

            List<string[]> people = new List<string[]>();
            while (select != 3)
            {
                Console.WriteLine("1. Add new");
                Console.WriteLine("2. View records");
                select = int.Parse(Console.ReadLine());

                switch (select)
                {
                    case 1:


                        string salary, pno, pcode, state, addr, strno, name;

                        Console.WriteLine("enter salary:");
                        salary = Console.ReadLine();

                        Console.WriteLine("enter phone number: ");
                        pno = Console.ReadLine();

                        Console.WriteLine("enter post code: ");
                        pcode = Console.ReadLine();

                        Console.WriteLine("enter state: ");
                        state = Console.ReadLine();

                        Console.WriteLine("enter adress: ");
                        addr = Console.ReadLine();

                        Console.WriteLine("enter street number: ");
                        strno = Console.ReadLine();

                        Console.WriteLine("enter name: ");
                        name = Console.ReadLine();

                        // You can add as many people as you want

                        people.Add(new string[] { name, salary, pno, pcode, state, addr, strno });
                        break;
                    case 2:
                        for (int i = 0; i < people.Count; i++)
                        {
                            Console.WriteLine(people[i][0] + "  " + people[i][1]);
                        }

                        break;
                    case 3:
                        break;

                }
            }


        }

Thanks for the tip, but for this we have been told specifically to use arrays rather then lists. Would you mind posting the array equivalent? :)

Thanks for the tip, but for this we have been told specifically to use arrays rather then lists. Would you mind posting the array equivalent? :)

static void Main(string[] args)
        {
            int select = 0;
            const int MAX_PEOPLE=10;
            string[][]people = new string[MAX_PEOPLE][];
            int count = 0; 
            while (select != 3)
            {
                Console.WriteLine("1. Add new");
                Console.WriteLine("2. View records");
                select = int.Parse(Console.ReadLine());

                switch (select)
                {
                    case 1:
                        if (count >= MAX_PEOPLE)
                        {
                            Console.WriteLine("No room for more peoples....");
                            break;
                        }

                        string salary, pno, pcode, state, addr, strno, name;

                        Console.WriteLine("enter salary:");
                        salary = Console.ReadLine();

                        Console.WriteLine("enter phone number: ");
                        pno = Console.ReadLine();

                        Console.WriteLine("enter post code: ");
                        pcode = Console.ReadLine();

                        Console.WriteLine("enter state: ");
                        state = Console.ReadLine();

                        Console.WriteLine("enter adress: ");
                        addr = Console.ReadLine();

                        Console.WriteLine("enter street number: ");
                        strno = Console.ReadLine();

                        Console.WriteLine("enter name: ");
                        name = Console.ReadLine();

                        // You can add as many people as you want
                      
                        people[count++]=new string[] { name, salary, pno, pcode, state, addr, strno };
                        break;
                    case 2:
                        for (int i = 0; i <count; i++)
                        {
                            Console.WriteLine(people[i][0] + "  " + people[i][1]);
                        }

                        break;
                    case 3:
                        break;

                }
            }


        }
commented: Patient :) +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.