[英]How do you create an Array for every Object and add it to a List (c#)
What I'm trying to do is create multiple Objects of the same class, each with their own set of values (eg a name, an address and a personal number) saved in an array, and later add it to a list of employees but when I try to create an Object the name and address stay as null. 我想做的是创建同一类的多个对象,每个对象都有自己的一组值(例如名称,地址和个人编号)保存在数组中,然后将其添加到员工列表中,但是当我尝试创建对象时,名称和地址保持为空。 This is what I have so far:
这是我到目前为止的内容:
abstract class Person
{
static public string _name;
static public string _adresse;
}
class Student : Person
{
#region fields
public string _personalnumber = Fakultät.generate_personalnr();
#endregion
//initiating the array witht the Infos about the Student
public string[] StudentenA = new string[3] { Person._name, Person._address, personalnumber };
#region const
public Studenten (string name, string address)
{
Person._name = name;
Person._address = address;
}
#endregion
Thanks in advance for your help. 在此先感谢您的帮助。 Bearowl
owl
edit 1: changed the variables to all English (all German in original Project) 编辑1:将变量更改为所有英语(原始项目中的所有德语)
Since it looks like a homework, I am not writing out the exact code but try to help you with some hints. 由于它看起来像是一项家庭作业,因此我没有写出确切的代码,而是尝试为您提供一些提示。
The Person class is probably not written properly. Person类可能未正确编写。
static
. static
。 The Student class is probably not written properly. 学生课可能写作不正确。
Edit: I have a proof of concept (tio.run) fix with examples for class inheritance (method override, etc.). 编辑:我有一个概念证明(tio.run)修复,其中包含类继承示例(方法重写等)。 Please let me know if I should share it here.
请让我知道是否可以在这里分享。 Thanks!
谢谢!
You should assign StudentenA
in the constructor, so that it takes place after setting the _name
and _address
. 您应该在构造函数中分配
StudentenA
,以便它在设置_name
和_address
之后_address
。
public Studenten (string name, string address)
{
Person._name = name;
Person._address = address;
StudentenA = new string[3] { Person._name, Person._address, personalnumber };
}
But also, _name
and _address
of Person shouldn't be static. 但是,Person的
_name
和_address
也不应该是静态的。 With static, only one instance of them exists - not all persons have the same name and address, do they? 使用static时,它们只有一个实例存在-并非所有人的名字和地址都一样,是吗? :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.