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.

6
  • 1
    If your instructor is using ArrayLists, then he needs to step into 2005 and get into generics. Commented Oct 3, 2011 at 9: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<T> 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. Commented Oct 3, 2011 at 9:30
  • You'll need some kind of UI for that, for example WinForms, WPF or WebForms. Commented Oct 3, 2011 at 9:31
  • 1
    @kaze A console application is a UI. Commented Oct 3, 2011 at 9:32
  • @joeenos Maybe the next chapter uses List<> and asks for the differences... Commented Oct 3, 2011 at 9:43

3 Answers 3

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);

...
Sign up to request clarification or add additional context in comments.

Comments

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);

3 Comments

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: 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.
Finding a better instructor would be a great idea, but unfortunately it is not my vote and I have to make this his way :/
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.