簡體   English   中英

在C#中從int轉換為bool

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

int vote;

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

對於此方法調用,我想將vote轉換為bool 我怎么轉換它?

這是方法簽名:

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

你可以嘗試類似的東西

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

假設1被投票,0被投票,或類似的東西。

從int到boolean

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

從布爾到int

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

它不會取決於你的程序的語義。 theThumbUp表示什么? 商業規則可能會說,如果有一個投票(即投票> 0),那么它的大拇指或者它可能會說如果有至少5票(投票> = 5)那么只有它的“豎起大拇指”。 所以基於你的呼叫會改變 - 但它會是這樣的

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

n是豎起大拇指所需的票數。

如果投票是真的,如果它們大於零,那么你將它們轉換為bool:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM