当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to use class method to stop loop?

问题描述:

I have class:

public class myclass {

bool b;

public bool notEnough {

if (this.b) { return true; }

else { return false;)

}

}

And try:

myclass obj = new myclass;

obj.b = true;

while (obj.notEnough) {

Thread.Sleep(5);

}

In some reason here is error in (obj.notEnough). How to do it right?

网友答案:

You want to call a method, so you would have to add the parenthesis:

myclass obj = new myclass();
obj.b = true;
while (obj.notEnough()) { //Methods are always called by using the parenthesis ()
  Thread.Sleep(5);
}
网友答案:

This causes infinite while loop. Below is the compiled code

class Program
{
    static void Main(string[] args)
    {
        myclass obj = new myclass();
        obj.b = true;
        while (obj.notEnough())
        {
            Thread.Sleep(5);
        }
    }
}

public class myclass
{
    public bool b;

    public bool notEnough()
    {
        if (this.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: