0

I have a problem with ArrayList in C#, I know how can I add my own input and run the program so the Array is filled with information. But I would like to fill an ArrayList based on user input.

This is what I need: the user could enter his/hers name, birthdate and age. And all these theree information would be stored in one element.

My goal is to be able to make an application which would allow the user to enter this kind of data for several people and then print the output.

Here is my code:

I have a class Person which handles the user information:

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

namespace ArrayList_Siomple_Sorted_10
{
    class Person
    {
    private string personName;
    private DateTime birthDate;
    private double personAge;

    public Person(string name, DateTime bDate, double age)
    {
        personName = name;
        birthDate = bDate;
        personAge = age;
    }
    public string Name
    {
        get { return personName; }
        set { personName = value; }
    }
    public DateTime Birthdate
    {
        get { return birthDate; }
        set { birthDate = value; }
    }

    public double Grade
    {
        get { return personAge; }
        set { personAge = value; }
    }

    public void Show()
    {
        Console.WriteLine(personName + " " + birthDate.ToString("d.M.yyyy") + " " + personAge);
    }
}
}

And this is the Main class with main method:

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

namespace ArrayList_Siomple_Sorted_10
{
class Program
{
    static void Main(string[] args)
    {
        DateTime birthDateP3 = new DateTime(1980, 2, 25);
        Person p3 = new Person("Ann Person", birthDateP3, 8);
        DateTime birthDateP2 = new DateTime(1980, 2, 25);
        Person p2 = new Person("Ann Person", birthDateP2, 8);
        DateTime birthDateP1 = new DateTime(1980, 2, 25);
        Person p1 = new Person("Ann Person", birthDateP1, 8);

        ArrayList ar = new ArrayList();

        ar.Add(p1);
        ar.Add(p2);
        ar.Add(p3);
        Console.WriteLine("Print the original Array");
        foreach (Person pr in ar)
            pr.Show();

    }
}
}

Is the thing I am trying to achieve even possible? Thank you for your answers. V.

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
Vojtech
  • 643
  • 3
  • 16
  • 30
  • 1
    If your instructor is using `ArrayList`s, then he needs to step into 2005 and get into generics. – Joe Enos Oct 03 '11 at 09:28
  • 1
    You need to get input from the user, so look as Console.Read... Then you need to turn the string they enter into the types for the properties on you Person object, the rest you have already done. You may want to look at List rather than array list but it rather depends on how far you are through your course. Also look at auto properties, which will save you some typing. Also find anyone's coding standards and read them, there's plenty on-line. – Adam Straughan Oct 03 '11 at 09:30
  • You'll need some kind of UI for that, for example WinForms, WPF or WebForms. – kaze Oct 03 '11 at 09:31
  • 1
    @kaze A console application is a UI. – Joe Enos Oct 03 '11 at 09:32
  • @joeenos Maybe the next chapter uses `List<>` and asks for the differences... – H H Oct 03 '11 at 09:43
  • @JoeEnos Yes I know, I could not read that from the original question, so I figured that to answer the question, it would be important to know. – kaze Oct 04 '11 at 06:09

3 Answers3

1

Yes - it's possible. What is the exact issue that are you facing here? BTW, instead of ArrayList, you should be using generic collection List<Person>.

From console program, you will use Console.ReadLine to get user input, validate/parse it and fill person instance and add to your list. For example,

...
    var ar = new List<Person>();

    var name = Console.ReadLine();
    // validate name (check if its not blank string etc)
    ...

    var dob = Console.ReadLine();
    // validate date of birth (date/time format, past date etc)
    ...
    DateTime dateOfBirth = DateTime.Parse(dob);

    // compute age
    var age = (DateTime.Now - dateOfBirth).Years;

    var p = new Person(name, dateOfBirth, age);
    ar.Add(p);

...
VinayC
  • 47,395
  • 5
  • 59
  • 72
0

First of all, you should consider using List<T> instead of ArrayList.

Secondly, off course, this is possible. You can do it in the following way.

static void Main(string[] args)
{
    DateTime birthDateP3 = new DateTime(1980, 2, 25);
    Person p3 = new Person("Ann Person", birthDateP3, 8);
    DateTime birthDateP2 = new DateTime(1980, 2, 25);
    Person p2 = new Person("Ann Person", birthDateP2, 8);
    DateTime birthDateP1 = new DateTime(1980, 2, 25);
    Person p1 = new Person("Ann Person", birthDateP1, 8);

    ArrayList ar = new ArrayList();

    string name = Console.ReadLine();        
    ar.Add(name);


    Console.WriteLine("Print the original Array");
    foreach (Person pr in ar)
        pr.Show();

}

And you should be using Generic list instead of ArrayList like this:

List<string> mylist = new List<string>();
string str = Console.ReadLine();
mylist.Add(str);
Aamir
  • 14,882
  • 6
  • 45
  • 69
  • List is a nice thing, but I am supposed to make it using the ArrayList. But in the Main class I need that the users in the console his name, birthdate and age. So, the will be no DateTime birthDateP3 = etc. Only the input from the user. When the user inputs: Max Payne, 25.5.2005, 6 then this info will be stored in the first object of the arraylist. – Vojtech Oct 03 '11 at 09:32
  • @Vojtech: ArrayList shouldn't be used now since it has a hugely better alternative in the form of Generic List. If you are forced to do so, try finding a better instructor. – Aamir Oct 03 '11 at 09:34
  • Finding a better instructor would be a great idea, but unfortunately it is not my vote and I have to make this his way :/ – Vojtech Oct 03 '11 at 09:37
0

Sure, you can read input data using Console.ReadLine() method or if you want a more sophisticated interface you can use Windows Form API.

Massimo Zerbini
  • 3,125
  • 22
  • 22