【问题标题】:How can I set c# object property value when its "get" is called调用“get”时如何设置c#对象属性值
【发布时间】:2014-12-06 20:24:15
【问题描述】:

我只是想看看有没有办法可以做到这一点。我有两门课。一个作为属性存在于另一个中。所以在某种程度上它就像一个父子类关系..子对象的值但是我想在这一刻设置它的“get”是基于父对象中存在的值调用的..只有当它存在并且不为空。

如果子对象已经设置,我也不想设置它...我只是想让机制尽可能简单,这无非是想办法去做。

我的孩子班级是

class ChildObject
    {
        public int? some_val { get; set; }

    }

我的父类是

class ParentObject
    {
        public int? some_val { get; set; }
        public ChildObject child
        {
            get {
               //if the chld hasnt been set already
                if (child == null)
                {
                    //if the value of the integer has been set
                    if(some_val != null)
                    {
                        //some some method that sets the objects value
                        //based on teh value of the integer property
                        SomeMethod();
                    }
                }
                return child;
            }
            set { child = value;}
        }
        private void SomeMethod()
        {
            int new_val = this.some_val.Value + 5;
            this.child = new ChildObject {  some_val = new_val };
        }
    }
}

在其他一些例程中......比如控制台应用程序或我执行以下操作的任何程序

  ParentObject p = new ParentObject ();
            p.some_val = 1;
            ChildObject c = p.child;
            int i = c.some_val.Value;

理想情况下,我想添加一些检查以查看父对象中的“some_val”是否已更改,如果更改则重新“设置”子对象......但现在我只是想弄清楚如何设置首次调用 get 时的属性。

由于某种原因,当我运行它时它只是崩溃了。而且没有任何例外。我尝试将例程包装在 try catch 中以查看问题所在,但它只是停止运行并关闭执行控制台程序。

【问题讨论】:

  • 它崩溃的原因是因为你这里有堆栈溢出问题。您的child 属性获取器读取child 属性以检查null。这意味着如果您读取child 属性,它将读取child 属性,它将读取child 属性,它将读取child 属性等。相反,向您的类添加一个支持字段并在 getter 和 setter 中使用此字段而不是属性本身。然后你就可以解决你以后想问的实际问题了。

标签: c#


【解决方案1】:

这里的问题是,当您想向getset 方法添加一些逻辑时,您需要包含一个包含实际值的字段。出现问题是因为您实际上在那里有一个无限循环并且可能会出现 SO 异常。发生这种情况是因为当您获得 child 时,您在这里再次调用它的吸气剂:return _child;

        public int? some_val { get; set; }
        private ChildObject _child;
        public ChildObject child
        {
            get {
               //if the chld hasnt been set already
                if (_child == null)
                {
                    //if the value of the integer has been set
                    if(some_val != null)
                    {
                        //some some method that sets the objects value
                        //based on teh value of the integer property
                        SomeMethod();
                    }
                }
                return _child;
            }
            set { _child = value;}
        }
        private void SomeMethod()
        {
            int new_val = this.some_val.Value + 5;
            this.child = new ChildObject {  some_val = new_val };
        }

【讨论】:

    【解决方案2】:

    您应该小心相同的字段或值名称。您应该尝试其他值和字段名称。像这样;

    private ChildObject child;
    public ChildObject Child
    {
        get {
           //if the chld hasnt been set already
            if (child == null)
            {
                //if the value of the integer has been set
                if(some_val != null)
                {
                    SomeMethod();
                }
            }
            return child;
        }
        set { child = value;}
    }
    

    你应该资源到封装原则。 祝你好运。

    【讨论】:

      【解决方案3】:

      你可以使用另一个字段来做这样的事情

          public int? some_val { get; set; }
          public ChildObject _child;
          public ChildObject child
          {
              get {
                 //if the chld hasnt been set already
                  if (_child == null)
                  {
                      //if the value of the integer has been set
                      if(some_val != null)
                      {
                          SomeMethod();
                      }
                  }
                  return _child;
              }
              set { _child = value;}
          }
      
      
      
          private void SomeMethod()
          {
              int new_val = this.some_val.Value + 5;
              this.child = new ChildObject {  some_val = new_val };
          }
      

      【讨论】:

        猜你喜欢
        • 2019-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多