简体   繁体   English

getter和setter方法的问题

[英]Issue with getters and setters Methods

In my CustomView class, TaskText and ProjectText are comboboxes. 在我的CustomView类中, TaskTextProjectText是组合框。 See the following property written for getting value from TaskText . 请参阅以下为从TaskText获得价值而编写的属性。 My TaskText combobox contains list of Tasks. 我的TaskText组合框包含任务列表。 When I select specific task at runtime, it should store id of that Task which I've done in setters. 当我在运行时选择特定任务时,它应该存储我在设置器中完成的任务的ID。 But I don't understand, how do I get that id? 但是我不明白,我怎么获得那个身份证?

Later, I'll convert that id to integer and passed to method. 稍后,我将把该id转换为整数并传递给方法。

See the below code, which I've tried- 请参阅以下我尝试过的代码-

public Harvest_Task taskClass
{
    get
    {
        return new Harvest_Task { _id = Int32.Parse(TaskText.Text) }; // Here _id doesn't take the value.
    }
    set
    {
        if (value != null)
        {
            TaskText.Text = (value._id).ToString();             
        }
    }
} 

通常,您为类的特定属性而不是整个类本身创建一个getter / setter方法...再加上您尝试做事的方式,我建议尝试Singleton类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM