如何使用自动实现的属性访问类? [英] How to access class with auto implemented property?

查看:61
本文介绍了如何使用自动实现的属性访问类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨盖兹,

为什么我的具有自动实现的属性的类获取空值?这是我所做的:

1.我使用自动实现的属性实例化该类,并设置以下值:

hi guyz,

why my class with auto implemented property gets null values? here''s what i did:

1. i instantiate the class with the auto-implemented property and set the values like:

class AIproperty
{
   public string CustomerName{ get; set; }

}

//Main class
AIproperty setvalues = new AIproperty();
setvalues.CustomerName = txtCustomer.text.ToString():


//other class
AIproperty setvalues = new AIproperty();
console.write(setvalues.CustomerName)



console.write(setvalues.CustomerName)
返回空值

我该怎么办?

首先,-thanks



console.write(setvalues.CustomerName)
returns null value

what should i do?

-thanks in advance

推荐答案

首先,应在类构造函数中使用string.Empty初始化属性,以解决空值问题.

First of all, you should initialize the property with string.Empty in the class constructor which will solve the null value problem.

class AIproperty
{
   public string CustomerName{ get; set; }

   public AIproperty()
   {
      this.CustomerName = string.Empty;
   }
}




如果有帮助,请将其标记为答案




Mark it as answer if it is helpful


AIProperty的实现正确.如果CustomerName在分配后返回null,则唯一的原因是:分配的值为null.在您的代码中,分配的值是txtCustomer.text.ToString().检查一下:它必须是null.

顺便说一句,ToString是多余的,因为txtCustomer.text可能是字符串类型.

-SA
The class AIProperty is implemented correctly. If CustomerName returns null after assignment, the only reason is: assigned value was null. In your code the assigned value was txtCustomer.text.ToString(). Check it up: it must be null.

By the way, chances are, ToString is redundant as txtCustomer.text might be of the string type.

—SA


它仍然返回空值,txtCustomer.text具有值,但是当涉及到AIProperty时,它仍然返回空值,我也尝试过这个,

it still returns null value, txtCustomer.text has value, but when it comes to AIProperty it still returns null value, i also tried this,

class AIproperty
{
   public string CustomerName{ get; set; }
 
   public AIproperty()
   {
      this.CustomerName = string.Empty;
   }
}



当另一个类获得该值时,它仍返回空值,请帮助.



when the other class gets the value it still returns null value,, help pls.


这篇关于如何使用自动实现的属性访问类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆