How to convert “0” and “1” to false and true

前端 未结 9 660
無奈伤痛
無奈伤痛 2021-02-06 19:59

I have a method which is connecting to a database via Odbc. The stored procedure which I\'m calling has a return value which from the database side is a \'Char\'. Right now I\'

相关标签:
9条回答
  • 2021-02-06 20:27
    (returnValue != "1" ? false : true);
    
    0 讨论(0)
  • 2021-02-06 20:33

    My solution (vb.net):

    Private Function ConvertToBoolean(p1 As Object) As Boolean
        If p1 Is Nothing Then Return False
        If IsDBNull(p1) Then Return False
        If p1.ToString = "1" Then Return True
        If p1.ToString.ToLower = "true" Then Return True
        Return False
    End Function
    
    0 讨论(0)
  • 2021-02-06 20:34

    How about:

    return (returnValue == "1");
    

    or as suggested below:

    return (returnValue != "0");
    

    The correct one will depend on what you are looking for as a success result.

    0 讨论(0)
  • 2021-02-06 20:37

    Or if the Boolean value is not been returned, you can do something like this:

    bool boolValue = (returnValue == "1");
    
    0 讨论(0)
  • 2021-02-06 20:44

    In a single line of code:

    bool bVal = Convert.ToBoolean(Convert.ToInt16(returnValue))
    
    0 讨论(0)
  • 2021-02-06 20:44

    If you don't want to convert.Just use;

     bool _status = status == "1" ? true : false;
    

    Perhaps you will return the values as you want.

    0 讨论(0)
提交回复
热议问题