运营商和放大器;&安培;不能应用 [英] Operator && cannot be applied

查看:119
本文介绍了运营商和放大器;&安培;不能应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试的例子片段,我发现作为<一个答案href="http://stackoverflow.com/questions/3920551/permission-deny-mask-in-sharepoint/4320497#4320497">another问

I am testing an example snippet that I found as an answer on another Question

但是编译器吐出这种运营商和放大器;&功放;不能应用于类型为长和布尔操作数。 它为什么要这样做呢?当我读了code,它说:如果面具和权限大于0成功返回布尔

However the compiler is spitting out this "Operator && cannot be applied to operands of type long and bool". Why does it do this? As I read the code, it says "If mask and permission are greater than 0 return success bool"

我是读这错了吗?

(同样,没有人叫出来一个坏榜样,所以我希望它的工作。这并不是说我是一个复制粘贴codeR)

(Also, no one called it out as a bad example so I expected it to work. Not that I am a copy-paste coder)

bool CheckMask( long Mask, long TestPermission ) {
    return Mask && TestPermission > 0;
}

long mask = 4611686844973976575;

const long MASK_ViewListItems = 0x0000000000000001;
bool HasPermission_ViewListItems = CheckMask(mask, MASK_ViewListItems);
// HasPermission_ViewListItems is true

const long MASK_UseClientIntegration = 0x0000001000000000;
bool HasPermission_UseClientIntegration = CheckMask(mask, MASK_UseClientIntegration);
// HasPermission_UseClientIntegration is false

有非常多的关于StackOverflow的类似的问题,我已经点击过大部分,有一个大的名单给我的权利,我打字。无已应用到我的情况,至少我能看到的答案,我的问题之间的关系。

There are an awful lot of similar questions on StackOverflow and I've clicked through most of them, there's a big list to my right as I type. None have applied to my situation, at least I was able to see the relation between the answers and my problem.

推荐答案

您正在使用的 &功放;&安培; (有条件的,仅适用于布尔操作数),而不是<一 HREF =htt​​p://msdn.microsoft.com/en-us/library/sbf85k1c.aspx相对=nofollow> &安培; (按位与,有效期布尔操作数或整数操作数) - 我怀疑你希望是后者,你也应该使用因precedence规则支架。我想改变参数的名称遵循.NET命名约定,太 - 并使其静态的,因为它不依赖于任何状态:

You're using && (conditional AND, only valid for bool operands) instead of & (bitwise AND, valid for bool operands or integer operands) - I suspect you want the latter, and you should also use brackets due to precedence rules. I'd change the parameter names to follow .NET naming conventions, too - and make it static as it doesn't rely on any state:

static bool CheckMask(long mask, long testPermission)
{
    return (mask & testPermission) > 0;
}

您可能还需要更改为使用的枚举,而不是

You might also want to change to using an enum instead of long:

[Flags]
public enum Permissions
{
    ViewListItems = 1 << 0,
    ...
    UseClientIntegration = 1 << 9
}

static bool CheckMask(Permissions mask, Permissions testPermission)
{
    return (mask & testPermission) != 0;
}

这篇关于运营商和放大器;&安培;不能应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆