Nothing = String.Empty (Why are these equal?)

前端 未结 4 959
春和景丽
春和景丽 2020-12-01 15:58

Why does the first if statement evaluate to true? I know if I use \"is\" instead of \"=\" then it won\'t evaluate to true. If I replace String.Empty with \"Foo\" it doesn\

4条回答
  •  萌比男神i
    2020-12-01 16:33

    Related to this topic, if you use a string variable initialized with "nothing" to be assigned to the property "value" of a SqlParameter that parameter is ignored, not included in the command sent to the server, and a missing parameter error is thrown. If you initialize that variable with string.empty everything goes fine.

    //This doesn't work
    Dim myString as String = nothing
    mySqlCommand.Parameters.Add("@MyParameter", SqlDbType.Char).Value = myString
    
    //This works    
    Dim myString as String = string.empty
    mySqlCommand.Parameters.Add("@MyParameter", SqlDbType.Char).Value = myString
    

提交回复
热议问题