Conditional Operator in C#
-
Hi, I'm trying to use the Conditional Operator in C# in the form of
<bool condition> ? <true value> : <false value>;
and I want the value to return as NULL if the condition is false. For example:bool _validated = _boolText != null? bool.Parse(_boolText): null;
But an error occurs when trying to build. Here is the error message:Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and '<null>'
How can I solve this problem? Thank you very much.KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --
-
Hi, I'm trying to use the Conditional Operator in C# in the form of
<bool condition> ? <true value> : <false value>;
and I want the value to return as NULL if the condition is false. For example:bool _validated = _boolText != null? bool.Parse(_boolText): null;
But an error occurs when trying to build. Here is the error message:Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and '<null>'
How can I solve this problem? Thank you very much.KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --
You must declare your boolean type variable to a nullable boolean, but this will only works in .NET 2005. bool? _validated = _boolText != null ? bool.Parse(_boolText): null; If you want to declare a nullable bool just add suffix "?" symbol to your datatype. ex. bool? int? string? regards, mark -- modified at 1:51 Friday 23rd February, 2007
-
You must declare your boolean type variable to a nullable boolean, but this will only works in .NET 2005. bool? _validated = _boolText != null ? bool.Parse(_boolText): null; If you want to declare a nullable bool just add suffix "?" symbol to your datatype. ex. bool? int? string? regards, mark -- modified at 1:51 Friday 23rd February, 2007
Hey that is cool. Thanks Mark. - Harini
-
Hey that is cool. Thanks Mark. - Harini
Here's an Article for Nullable Data Type. http://www.c-sharpcorner.com/UploadFile/PashuSX/NullableTypes04282006114548AM/NullableTypes.aspx[^]
-
You must declare your boolean type variable to a nullable boolean, but this will only works in .NET 2005. bool? _validated = _boolText != null ? bool.Parse(_boolText): null; If you want to declare a nullable bool just add suffix "?" symbol to your datatype. ex. bool? int? string? regards, mark -- modified at 1:51 Friday 23rd February, 2007
Wow! Thanx a lot, Mark. Never known there is this trick in C# 2.0. By the way, is there any way to convert from non-nullable to nullable? Say... I need to assign a value to a variable for web service. In my provided example, the variable for a web service is of bool type. How can i convert it to "bool?" ? Thank you very much.
KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --
-
Wow! Thanx a lot, Mark. Never known there is this trick in C# 2.0. By the way, is there any way to convert from non-nullable to nullable? Say... I need to assign a value to a variable for web service. In my provided example, the variable for a web service is of bool type. How can i convert it to "bool?" ? Thank you very much.
KiT -- Never wait for a chance to come, Believe in your own potential and go get it! --
The non-nullable bool have two posible values either True or false only. I think It's imposible that your web services will return a null value if does this will generate an error object not set to instance. Back to your question. Yes, you can, just cast it. bool? myvar = (bool?)MyWebServiceMethodBool(); Note: MyWebServiceMethodBool() will only return True or false because the data type of this method is non-nullable boolean the only posible value this method will return is either true or false. the new myvar variable can have three value (true,false and null) regards, Mark
-
The non-nullable bool have two posible values either True or false only. I think It's imposible that your web services will return a null value if does this will generate an error object not set to instance. Back to your question. Yes, you can, just cast it. bool? myvar = (bool?)MyWebServiceMethodBool(); Note: MyWebServiceMethodBool() will only return True or false because the data type of this method is non-nullable boolean the only posible value this method will return is either true or false. the new myvar variable can have three value (true,false and null) regards, Mark
I see. Thank you very much. You're the man. :-D
KiT
Never wait for a chance to come, Believe in your own potential and go get it!
-
The non-nullable bool have two posible values either True or false only. I think It's imposible that your web services will return a null value if does this will generate an error object not set to instance. Back to your question. Yes, you can, just cast it. bool? myvar = (bool?)MyWebServiceMethodBool(); Note: MyWebServiceMethodBool() will only return True or false because the data type of this method is non-nullable boolean the only posible value this method will return is either true or false. the new myvar variable can have three value (true,false and null) regards, Mark
Wow! I have given score 5 out of 5 for this. Harini