<namespace>是&#39;类型&#39;,在给定的上下文中无效

时间:2016-10-10 17:09:25

标签: c# namespaces syntax-error

我讨厌检查布尔值以查看状态,然后根据它切换它们,所以我写了一些东西给我做。

它一直给我以下错误:  是&#39;类型&#39;,在给定的上下文中无效

这是命名空间和类:

namespace Bool
{
    public class ToggleState
    {

        static bool Toggle(bool Bool)
        {
            if (Bool == true)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

以下是给出错误的代码

Test = Bool.ToggleState(Test);

2 个答案:

答案 0 :(得分:1)

这是因为你的命名空间被称为“Bool”,它使它成为一种类型。

我为你重构了一下。

    //example to group by propertyname 
    <table ng-controller="myCtrl" border="1">
    <tr ng-repeat="x in camps  | groupBy:'brandName' }">
    <td>{{x.campName}}</td>
    <td>{{x.campBudget}}</td>  
    </tr>

    //example to filter by a propertyValue
    <table ng-controller="myCtrl" border="1">
    <tr ng-repeat="x in camps  | filter:{ brandName: 'yourbrandname' }">
    <td>{{x.campName}}</td>
    <td>{{x.campBudget}}</td>  
    </tr>
    </table>

名称Bool(您当前的命名空间)很容易被struct bool(变量类型)弄错。考虑给它更有意义的名字。

答案 1 :(得分:0)

虽然毫无疑问命名空间命名并不是最好的,但如果您解决了以下问题,我认为没有任何理由不起作用:

1)你应该有namespace Bool //Your namespace, it's a type { public class ToggleState // Your class, also a type { static bool Toogle(bool boolValue) { return !boolValue; //revert bool value and return it back } } }

2)Test = Bool.ToggleState.Toggle(Test);方法应该是公开的