问题描述:

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;
        }
    }
}
相关阅读:
Top