[英]NullReferenceException after creating new parent class property
I would imagine this is quite a basic question in C#. 我想这是C#中的一个基本问题。 I've got my head in a bit of spin with it though and I'm unsure of the correct way of sorting it.
不过,我对此有些疑惑,但是我不确定如何对它进行排序。
I have a parent class with get/set properties and with a child class. 我有一个具有get / set属性的父类和一个子类。 When an instance of the class is created using new the property to the parent class is accessible but the child class isn't.
使用new创建类的实例时,可以访问父类的属性,但不能访问子类。 I remember in C programming you have to create the memory space for this but I'm unsure of the correct way of doing this in C#.
我记得在C编程中必须为此创建内存空间,但是我不确定在C#中执行此操作的正确方法。
Parent Class 家长班
class Parent_class
{
private int number;
public int Number
{
get { return number; }
set { number = value; }
}
private Child_class childclass;// = new Child_class();
public Child_class Childclass
{
get { return childclass; }
set { childclass = value; }
}
}
Child Class 儿童班
class Child_class
{
private int number;
public int Number
{
get { return number; }
set { number = value; }
}
}
Main 主要
static void Main(string[] args)
{
Parent_class test = new Parent_class();
test.Number = 3; //<--Ok
test.Childclass.Number = 4; //<--NullReferenceException
}
You don't need to use the field-backed getter / setter if you aren't doing anything special -- the compiler can create that for you. 如果您没有做任何特别的事情,则不需要使用字段支持的getter / setter方法-编译器可以为您创建它。
To obtain an instance of a class, you need to use new
. 要获取类的实例,您需要使用
new
。 Since it looks like you want Parent_class
to have an instance of child class automatically, you can do that in the constructor
. 由于看起来您希望
Parent_class
自动具有子类的实例,因此可以在constructor
执行此操作。
Oh - and the reason why Number works fine is that's a primitive
type, and not a class. 哦-Number正常工作的原因是
primitive
类型,而不是类。 Primitives, (int, float, bool, double, DateTime, TimeSpan, to name a few) do not need instantiation via new
. 基本体(int,float,bool,double,DateTime,TimeSpan等)不需要通过
new
实例化。
Parent Class 家长班
public class Parent_class
{
public Parent_class()
{
Childclass = new Child_class();
}
public int Number { get; set; }
public Child_class Childclass { get; set; }
}
Child Class 儿童班
public class Child_class
{
public int Number { get; set; }
}
Main 主要
static void Main(string[] args)
{
Parent_class test = new Parent_class();
test.Number = 3; //<--Ok
test.Childclass.Number = 4;
}
You have not created Instance of Child class. 您尚未创建Child类的实例。
You can do either of below 您可以执行以下任一操作
Initialize just before use 使用前初始化
static void Main(string[] args) { Parent_class test = new Parent_class(); test.Number = 3; //<--Ok test.ChildClass = new Child_class(); test.Childclass.Number = 4; //<--NullReferenceException }
2 . 2。 Initialize in the parent ctor
在父级ctor中初始化
public Parent_class() { Childclass = new Child_class(); }
3 . 3。 Initialize inline at the time of declaration
在声明时初始化内联
private Child_class childclass = new Child_class();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.