简体   繁体   English

在C#中从int转换为bool

[英]Converting from an int to a bool in C#

int vote;

Insertvotes(objectType, objectId , vote, userID); //calling

For this method call, I want to convert vote to a bool . 对于此方法调用,我想将vote转换为bool How can I convert it? 我怎么转换它?

Here is the method signature: 这是方法签名:

 public static bool Insertvotes(int forumObjectType, 
                                int objectId,
                                bool isThumbUp, 
                                int userID) 
{
    // code...
}

You can try something like 你可以尝试类似的东西

Insertvotes(objectType, objectId , (vote == 1), userID); //calling

Assuming that 1 is voted up, and 0 is voted down, or something like that. 假设1被投票,0被投票,或类似的东西。

To go from int to boolean 从int到boolean

bool isThumbsUp = Convert.ToBoolean(1); //Will give you isThumbsUp == true

To go from boolean to int 从布尔到int

int isThumbsUp = Convert.ToInt32(false); //Will give you isThumbsUp == 0

Wouldn't it depend upon semantics of your program. 它不会取决于你的程序的语义。 What does isThumbUp indicate? theThumbUp表示什么? A business rule may say that if there is one vote (ie vote > 0) then its thumbs up or it may say that if there are minimum 5 votes (vote >= 5) then only its "thumbs up". 商业规则可能会说,如果有一个投票(即投票> 0),那么它的大拇指或者它可能会说如果有至少5票(投票> = 5)那么只有它的“竖起大拇指”。 So based on that your call will change - but it would be something like 所以基于你的呼叫会改变 - 但它会是这样的

Insertvotes(objectType, objectId , (vote > [n]), userID);

n being the number of votes needed for thumbs up. n是竖起大拇指所需的票数。

如果投票是真的,如果它们大于零,那么你将它们转换为bool:

Insertvotes(objectType, objectId , vote > 0, userID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM