Reputation: 2174
I'm trying to save sole lines of code in my VB.NET project using ternary If
operator, but getting thrown:
"Specified Cast is no valid"
' THIS WORKS FINE
If info.GetString("IdentificationType").Trim = "" Then
_Expiration = System.Data.SqlTypes.SqlDateTime.MinValue
Else
_Expiration = info.GetValue("Expiration", Expiration.GetType)
End If
' THIS THROWS AN EXCEPTION
_Expiration = If(info.GetString("IdentificationType").Trim = "",
System.Data.SqlTypes.SqlDateTime.MinValue,
info.GetValue("Expiration", Expiration.GetType))
Upvotes: 0
Views: 54
Reputation: 501
System.Data.SqlTypes.SqlDateTime.MinValue is of type System.Data.SqlTypes.SqlDateTime
If I have to guess, Expiration.GetType must be of the type System.DateTime.
Add cast CDate(System.Data.SqlTypes.SqlDateTime.MinValue)
Try this,
_Expiration = If(info.GetString("IdentificationType").Trim = "",
CDate(System.Data.SqlTypes.SqlDateTime.MinValue),
info.GetValue("Expiration", Expiration.GetType))
Upvotes: 1
Reputation: 19330
Your problem is
System.Data.SqlTypes.SqlDateTime.MinValue
and
info.GetValue("Expiration", Expiration.GetType)
return different types. This is as much answer as I can give without knowing variables and return types.
Bottom line, when you use If(1, 2, 3)
- 2 and 3 must return same type
Upvotes: 1