开发者

C# arraylist user input

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.


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

...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜