Whats wrong with this syntax? I can't figure it out for the life of me!
String str = ((String)result[i][1] ? (String)result[i][1] : ""); Error 7 Cannot implicitly convert type 'string' to 'bool'
its doing me trunk in :p
Whats wrong with this syntax? I can't figure it out for the life of me!
String str = ((String)result[i][1] ? (String)result[i][1] : ""); Error 7 Cannot implicitly convert type 'string' to 'bool'
its doing me trunk in :p
Short answer: the ternary operator requires a boolean condition, so you cannot use a string value as the condition. 's code is trying to use the same string both as the condition and the chosen value, which is why the compiler complains. was on the right track suggesting an explicit test; 's idea of changing types won't fix the logic, and 's ternary syntax note is useful but remember C# requires a true/false expression (unlike C/C++).
If the goal is "use the string if it exists, otherwise use an empty string", these patterns are safer and clearer:
string str = result[i][1] as string ?? string.Empty; This attempts a safe cast and falls back to an empty string if the cast fails or the value is null.
string str = result[i][1] is string s ? s : string.Empty; This uses pattern matching (C# 7+) to both test and capture the string in one step.
If the source can contain database nulls (DBNull.Value), check explicitly before calling ToString:
object val = result[i][1];
string str = val == DBNull.Value ? string.Empty : val?.ToString() ?? string.Empty; Notes and cautions: validate result and result[i] exist before indexing to avoid exceptions. Choose the approach that matches the actual runtime types (if the items are already strings, as or is is fine; if they can be other value types, decide whether ToString() is appropriate). For the language rules see the C# docs on the conditional operator and the null-coalescing operator:
conditional operator
null-coalescing operator.
Jump to Post— Poab9200 6If I'm not mistaken I believe its telling you that its not in a String format its in a Bool format.
Substitute String for Bool and see what happens...
If it doesn't work then I'm sorry, and I hope someone can help resolve this issue for you. I …
If I'm not mistaken I believe its telling you that its not in a String format its in a Bool format.
Substitute String for Bool and see what happens...
If it doesn't work then I'm sorry, and I hope someone can help resolve this issue for you. I am new to C# so don't get angry at me. I just made the assumption looking at the error message.
- Poab9200
The condition must evaluate to a boolean type. Unless result[1] is already a boolean type, you can't use it without a relational test:
String str = ( (String)result[i][1] != null ? (String)result[i][1] : "" ); Also, what type is result[1] such that a cast to String is required?
the syntax is
<boolean expression> ? <staement1> : <statement2>;
now here depending on the value of boolean expression one statement is choosen for execution. if this is in C/C++ it can be any boolean expression or any statement with some integer value or no value at all(in this case it is considered true always).
but if the code is for java,it should essentially be a boolean expression.
>in C/C++
This is the C# forum.
>it can be any boolean expression or any statement with some integer value
IIRC, the description is "any expression that can be contextually converted to bool".
>or no value at all(in this case it is considered true always)
This is incorrect. Failure to provide a condition expression is a syntax error.
>but if the code is for java
This is still the C# forum. Conveniently enough for you, the Java rule matches C#.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.