开发者

visual studio 2010 crashing when build happens

开发者 https://www.devze.com 2023-03-09 21:22 出处:网络
my part of code is public int MyProperty { set { DoTask(); } } private void DoTask() { int MyValue = MyP开发者_如何学Goroperty;

my part of code is

public int MyProperty { set { DoTask(); } }

private void DoTask()
{
    int MyValue = MyP开发者_如何学Goroperty;
}

I don't have Get accessor for MyProperty. I tried to get the value of it in DoTask(). When I build this application in VS2010, it is crashing insted of giving a build Error. Isn't this an error? Correct me if I'm wrong or Misunderstood.

Regards Umesh C


I found issues with code you have written

public int MyProperty { set { DoTask(value); } }

private void DoTask(int value)
{
    int MyValue = value;
}

or

public int MyProperty { set { int MyValue = value; } }

you code doesnt making any sense..


The code should clearly NOT cause an infinite loop, as the Getter for MyProperty does not exist (read: not even private). The compiler should detect this.

Nevertheless, a better design would be to provide a public getter for public settable properties. What your code will do is de-facto calling a method, so why would you even need the property? Simply make your DoTask() method public and let clients call it directly. Remember, with your code, you had absolutely no chance to get the value of MyProperty, not even from within your class.


its clear it causes an infinite loop my property calls dotask and dotask calls myporpety and it crashes the compiler the solution that Pranayprovided is good idea try to use

so you should the way define the property change it to

private int m_MyProperty;
public int MyProperty { get {return m_MyProperty}set { DoTask(); m_MyProperty=value} }

private void DoTask() { int MyValue = m_MyProperty; }

DoTask Always reads previos value of MyProperty

0

精彩评论

暂无评论...
验证码 换一张
取 消