简体   繁体   English

创建对象后设置其属性

[英]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 ,我是否可以自动设置此设置?

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而不会设置自动规则来影响它

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.

相关问题 在静态属性getter中创建的新对象是否只创建一次? - Is a new object that's created in a static property getter only created once? 改变模拟 <IType> 对象。之后调用.Object属性 - Alter Mock<IType> object after .Object property has been called 您如何测试已创建对象的新实例? - How do you test that a new instance of an object has been created? 检查从哪个 Class 创建了 object - Check from which Class the object has been created 一旦该属性已传递给 C# 中的 function,我该如何更改该属性 - How can I change the property once that property has already been passed to a function in C# 从一直匿名的对象中提取单个属性? - Extract a single property from an object that has always been anonymous? Sharepoint客户端对象模型属性或字段尚未初始化 - Sharepoint Client Object Model The property or field has not been initialized 一旦另一个类通过CA Transition设置了Xamarin iOS中的视图控制器上的隐藏属性 - How to set a hidden property on a view controller in Xamarin iOS once it has been set by another class with a CA Transition 创建另一个文件后未创建文件 - File is not created after another has been created 创建边框对象后,如何在Windows Phone 7中更改边框对象的属性(如颜色) - How to change the border object's property such as color in Windows Phone 7, once border object is created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM