使用对象填充数组(dateTime)

时间:2012-06-04 09:32:01

标签: c#

我的错误是在我的for语句中,语法错误是说我的构造函数没有采用三种方法。

        string[] first = new string[20] { "Scott", "Ramona", "Todd", "Melissa", "Naomi", "Leland", "Conor", "Julie", "Armondo", "Leah",
                                          "Frank", "Peter", "Ila", "Mandy", "Sammy", "Gareth", "Garth", "Wayne", "Freddy", "Mark" };

        string[] last = new string[20] { "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Kennedy", "Carrel", "MaloyTheBeautiful", "Johnson", "Smith", 
                                         "Sinatra", "Clemens", "Eels", "Johnson", "Eels", "Thompson", "Brooks", "World", "Crugar", "Thomas" };
        DateTime[] birth = new DateTime[20];
        birth[0] = new DateTime(1987, 22, 7);
        birth[1] = new DateTime(1962, 15, 9);
        birth[2] = new DateTime(1984, 21, 4);
        birth[3] = new DateTime(1977, 24, 1);
        birth[4] = new DateTime(1983, 12, 8);
        birth[5] = new DateTime(1979, 14, 1);
        birth[6] = new DateTime(1965, 19, 9);
        birth[7] = new DateTime(1968, 21, 2);
        birth[8] = new DateTime(1980, 22, 7);
        birth[9] = new DateTime(1982, 20, 7);
        birth[10] = new DateTime(1984, 19, 4);
        birth[11] = new DateTime(1968, 11, 9);
        birth[12] = new DateTime(1968, 21, 8);
        birth[13] = new DateTime(1975, 5, 2);
        birth[14] = new DateTime(1945, 15, 3);
        birth[15] = new DateTime(1969, 14, 6);
        birth[16] = new DateTime(1987, 141, 4);
        birth[17] = new DateTime(1976, 23, 5);
        birth[18] = new DateTime(1989, 28, 6);
        birth[19] = new DateTime(1988, 23, 9);

        // Populate Array Person[] People = new Person[20];     


        for (int i = 0; i < People.Length; i++)
        {
            People[i]= new Person(first[i], last[i], birth[i]);
        }

我的班级看起来像这样

public class Person
{
    private string _firstname;
    private string _lastname;
    private DateTime _birthDate;

    public void Personn(string firstname, string lastname, DateTime birthDate)
    {
        _firstname = firstname;
        _lastname = lastname;
        _birthDate = birthDate;
    }

    public string Firstname
    { get { return _firstname; } }

    public string Lastname
    { get { return _lastname; } }

    public DateTime Birthdate
    { get { return _birthDate; } }
}

5 个答案:

答案 0 :(得分:4)

而不是

 public void Personn(string firstname, string lastname, DateTime birthDate)

使用

 public Person(string firstname, string lastname, DateTime birthDate)

答案 1 :(得分:2)

您认为构造函数只是一种方法。你拼错了班级名字。

改变这个:

    public void Personn(string firstname, string lastname, DateTime birthDate)

为:

    public Person(string firstname, string lastname, DateTime birthDate)

答案 2 :(得分:0)

检查你的构造函数,它不应该有一个返回类型,它应该与类同名。

答案 3 :(得分:0)

我认为你应该使用而不是这一行:

 public void Personn(string firstname, string lastname, DateTime birthDate)

这一个:

 public Person(string firstname, string lastname, DateTime birthDate)

因为当前它是一个void方法而不是构造函数,但是你有一个默认的构造函数,在这种情况下没有参数。这可能是你问题的共鸣!

安德鲁

答案 4 :(得分:0)

构造函数没有任何返回类型。从构造函数中删除void

相关问题