Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Contract understanding of error

I am just getting started with Code Contracts, and need a little help in correcting an error:

Given this code:

class MyClass
{
    private bool _isUsed = false;

    public void SomeMethod()
    {
        Contract.Requires(!_isUsed);
    }
}

I get the following error:

error CC1038: Member 'MyClass._isUsed' has less visibility than the enclosing method 'MyClass.SomeMethod'

which seems to makes alot of the standard checks unavailable. What am I missing in this example?

like image 719
David Williams Avatar asked Jun 15 '11 19:06

David Williams


1 Answers

It has already been explained that either _isUsed has visibility issues (caller does not have control) which is rightly enforced by requires.

However, depending on what you are trying to accomplish with the Contract, Contract.Assert may meet your needs.

public void SomeMethod()
{
    Contract.Assert(!_isUsed);
}

would be valid while the Requires was not.

like image 53
vossad01 Avatar answered Nov 04 '22 11:11

vossad01