[英]Setting a property of an Object once it has been created
I have an object with 2 properties: 我有一个具有2个属性的对象:
public class Request
{
public int TypeId { get; set; }
public bool isApproved { get; set; }
}
What I wish to happen is, if TypeId
equals 1
, I want isApproved
to equal false
, otherwise I want it to equal true
when I create a new object. 我希望发生的是,如果
TypeId
等于1
,我希望isApproved
等于false
,否则在创建新对象时希望它等于true
。 I tried the following but it was set to true
for both of my objects, where I do the rule in the constructor: 我尝试了以下操作,但是对我的两个对象(在构造函数中执行规则的对象)都设置为
true
:
public Request() {
if(this.TypeId == 1) {
this.isApproved = false;
}
this.isApproved = true;
}
var request = new Request() {
TypeId = 1
}
var request2 = new Request() {
TypeId = 2
}
I know why this occurred, its because TypeId
hasn't been set when the constructor is called, so it defaults to true
. 我知道为什么会这样,这是因为在调用构造函数时尚未设置
TypeId
,所以它默认为true
。 Is there anyway I can set this automatically once TypeId
has been set on a newly created object? 无论如何,一旦在新创建的对象上设置了
TypeId
,我是否可以自动设置此设置?
I'd also like to have the option to change isApproved
manually at a later date, so if it was set to false
I can change it to true
without the automatic rule I set affecting it 我还希望以后可以手动更改
isApproved
的选项,因此,如果将其设置为false
,则可以将其更改为true
而不会设置自动规则来影响它
I would put the logic in the IsApproved
getter, and make it a read only value: 我将逻辑放在
IsApproved
getter中,并将其IsApproved
只读值:
public class Request
{
public int TypeId { get; set; }
public bool IsApproved
{
get
{
return this.TypeId != 1;
}
}
Oh, also C# code standards usually specify that properties should be CamelCased. 哦,C#代码标准通常还会指定属性应为CamelCased。
Edit: 编辑:
I'd also like to have the option to change isApproved manually at a later date, so if it was set to false I can change it to true without the automatic rule I set affecting it
我还希望以后可以手动更改isApproved的选项,因此,如果将其设置为false,则可以将其更改为true,而不会设置自动规则来影响它
You would want to set it with a constructor then. 然后,您需要使用构造函数进行设置。 (As Alex already suggested)
(如Alex所建议)
public class Request
{
public int TypeId { get; set; }
public bool IsApproved { get; set; }
public Request(int typeId)
{
this.TypeId = typeId;
this.IsApproved = typeId != 1;
}
}
Try changing 尝试改变
public int TypeId { get; set; }
to 至
public int _typeId;
public int TypeId
{
get
{
return _typeId;
}
set
{
_typeId = value;
isApproved = value != 1;
}
}
C# 4.0 allows you to assign a default value to method parameters, and makes you able to do this: C#4.0允许您为方法参数分配默认值,并使您能够执行以下操作:
public Request(int TypeId = 1)
{
approved = TypeId != 1;
}
usage: 用法:
var request = new Request(2); // approved = true
var request2 = new Request(1); //approved = false
public class Request
{
private int _typeId;
public int TypeId {
get { return _typeId; }
set {
_typeId = value;
isApproved = _typeId != 1;
}
}
public bool isApproved { get; private set; }
}
public class Request
{
private int _typeId;
public int TypeId
get
{
return _typeId;
}
set
{
isApproved = value != 1;
_typeId = value;
}
public bool isApproved { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.