Hey, So I'm working on a pretty basic program to help me get accustomed to classes, private variables and arrays.

I need to hold the data for 5 individuals such as name address email etc. These are held in my PersonClass and are private strings.

I access these strings using

public void setName(string Name)
        {
            this.name = Name;
        }

I now need to store these private strings in an array. I'm thinking it has to be a 2d array (Fields then each persons data) and I'm wondering how to enter the data into the arrays.

I'm using this code to read the input and place into the string

Console.WriteLine("Please Enter First Name.");
                            string input = Console.ReadLine();
                            Person.setFName(input);

Recommended Answers

All 10 Replies

Why do you need a 2D array if you already have a class holding the data?

personClass[] people = new personClass[N];

for (int i = 0; i < N; i++)
{
    people[i] = new personClass();

    people[i].Name = "John Doe";
    people[i].Address = "123 Nowhere Lane";
    people[i].Phone = "(555) 555-5555";
    people[i].PostCode = "12345";
    people[i].Height = "182cm";
}

Oh I hadn't thought about that. It makes sense, I'll try and implement it this way. Thank you.

So here's what I'm trying to do

case '1':

                            personClass[] person = new personClass[5];
                            for (int i = 0; i < 5; i++)
                            {
                                person[i] = new personClass();

                                Console.WriteLine("Please Enter First Name.");
                                string x = Console.ReadLine();
                                person[i] = personClass.setName(x);;
                            }
public void setName(string Name)
        {
            this.name = Name;
        }

When I change void to string I get an error also :S I'm not sure what return type I need to be using.

The errors I'm getting with this are

Namespace.Class.setname(string)':not all code paths return a value
+
cannot implicitly convert type 'string; to 'Namespace.Class.etc

You don't need a Setname method, if you have a class with a public field "Name".
Just use people.Name = Console.ReadLine();

The project requires me to use accessor methods for the data fields. This is the part I'm having trouble with, dealing with private variables and using get/set.

You are a bit off in this case RFID. If you want to learn array, dont use these kind od examples, there are way better ways to get the data from persons.

The simpliest way would be:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hi, please insert data for 5 people. Follow the instructions. thx.");
            Person[] ppl = new Person[5];
            for (int i = 0; i < ppl.Length; i++)
            {
                ppl[i] = new Person();
                Console.WriteLine("For " + (i + 1).ToString() + ". person type:");
                Console.Write("Name: ");
                ppl[i].Name = Console.ReadLine();
                Console.Write("Telephone: ");
                ppl[i].Telephone = Console.ReadLine();
                Console.Write("Heigh: ");
                ppl[i].Height = int.Parse(Console.ReadLine()); //be careful, if you will type a string, an exception will be thrown
            }
            Console.WriteLine("Thx for filling the data.");
            Console.ReadLine();
        }
    }

    class Person
    {
        public string Name { get; set; }
        public string Telephone { get; set; }
        public int Height { get; set; }
    }

Thank you for the example Mitja Bonca but I'm not sure we're on the same wavelength.

The project requires a class which is Person and a class for the main method PersonMain.

the Person class is required to have private data fields, name etc and to have accessor methods allowing us to use the private data fields in the main program but I need to use an array to store the 5 entries. Maybe I'll try starting again from scratch differently to what I've done.

Why use private fileds, if they are only visible from that class? Its pointless.

If you will use properties (like I did), they look like:

private string name;
public stirng Name
{
    get { return name; }
    set { name = value; }
}

in this say you can only acces to Name (public part of property), and not to name!
But I dont think you will use properties - and use them as private is pointless.
They can only be accessible from INSIDE this class.

So can I ask you who give you this task? Because its a bit strange (at least what you are trying to do).

commented: Well explained +15

I think that is probably what whoever set this particular piece of homework was intending - just incorrect terminology used.

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.