Inline If problem
-
Can anyone explain why the following won't compile and a possible solution... string strSequence = (view.GetFocusedValue().ToString() == String.IsNullOrEmpty ? view.GetFocusedValue().ToString() : ""); I get the following error... Error 16 Operator '==' cannot be applied to operands of type 'string' and 'method group'. Many Thanks
-
Can anyone explain why the following won't compile and a possible solution... string strSequence = (view.GetFocusedValue().ToString() == String.IsNullOrEmpty ? view.GetFocusedValue().ToString() : ""); I get the following error... Error 16 Operator '==' cannot be applied to operands of type 'string' and 'method group'. Many Thanks
String.IsNullOrEmpty is a method, not a property. Try to use this instead:
string strSequence = (string.IsNullOrEmpty(view.GetFocusedValue().ToString()) ? view.GetFocusedValue().ToString() : string.Empty);
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Can anyone explain why the following won't compile and a possible solution... string strSequence = (view.GetFocusedValue().ToString() == String.IsNullOrEmpty ? view.GetFocusedValue().ToString() : ""); I get the following error... Error 16 Operator '==' cannot be applied to operands of type 'string' and 'method group'. Many Thanks
-
stancrm wrote:
change to .Empty
Trust me, having if conditions that change variable contents is a good way to really mess your code up and cause such nasty bugs! I don't think Empty() does quite what you think it does...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Can anyone explain why the following won't compile and a possible solution... string strSequence = (view.GetFocusedValue().ToString() == String.IsNullOrEmpty ? view.GetFocusedValue().ToString() : ""); I get the following error... Error 16 Operator '==' cannot be applied to operands of type 'string' and 'method group'. Many Thanks
Ok wait, what are you trying to achieve? This code (even if I pretend that the == works) does not do anything useful. Suppose
view
is null: NullReferenceException Or the value returned byGetFocusedValue
is null: NullReferenceException Or the value returned byToString
is null: strSequence is nullToString
returns "": strSequence is ""ToString
returns anything else: strSequence is "" So you either get an exception, or you get a useless value in strSequence. What's the deal here? What did you want to do? If what you want is: the value of view.GetFocusedValue().ToString() if it isn't null, or "" if it is null Then you could do:string strSequence = view.GetFocusedValue().ToString() ?? "";
Disclaimer: I just woke up, it's entirely possible that I'm not making any sense at all - sorry! -
stancrm wrote:
change to .Empty
Trust me, having if conditions that change variable contents is a good way to really mess your code up and cause such nasty bugs! I don't think Empty() does quite what you think it does...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
string.Empty doesn't change the value of the string unless you assign it, and the OP was doing an equality test. What do you think it does? If his test is
if (value == string.Empty)
, this doesn't changevalue
."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
string.Empty doesn't change the value of the string unless you assign it, and the OP was doing an equality test. What do you think it does? If his test is
if (value == string.Empty)
, this doesn't changevalue
."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
String.IsNullOrEmpty is a method, not a property. Try to use this instead:
string strSequence = (string.IsNullOrEmpty(view.GetFocusedValue().ToString()) ? view.GetFocusedValue().ToString() : string.Empty);
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Indeed, but that's for a separate issue (i.e. it's quicker to check the length). The fuller check is to use
string.IsNullOrEmpty
to check because the value might be null or it might be empty, in which casevalue.Length
would throw anArgumentNullException
."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Ok wait, what are you trying to achieve? This code (even if I pretend that the == works) does not do anything useful. Suppose
view
is null: NullReferenceException Or the value returned byGetFocusedValue
is null: NullReferenceException Or the value returned byToString
is null: strSequence is nullToString
returns "": strSequence is ""ToString
returns anything else: strSequence is "" So you either get an exception, or you get a useless value in strSequence. What's the deal here? What did you want to do? If what you want is: the value of view.GetFocusedValue().ToString() if it isn't null, or "" if it is null Then you could do:string strSequence = view.GetFocusedValue().ToString() ?? "";
Disclaimer: I just woke up, it's entirely possible that I'm not making any sense at all - sorry!Harold, Thanks for that - I think that is exactly what I need. Basically all I want to do is check if View.GetFocusedValue... is null. If it is null then assign a "" otherwise use the View.GetFocusedValue. What will happen with it if the value comming back happens to be DBNull.Value (assuming I was doing something with a dataset. Thanks
-
Harold, Thanks for that - I think that is exactly what I need. Basically all I want to do is check if View.GetFocusedValue... is null. If it is null then assign a "" otherwise use the View.GetFocusedValue. What will happen with it if the value comming back happens to be DBNull.Value (assuming I was doing something with a dataset. Thanks
With the code I suggested you could still get a nasty NullRef, but how about
var FocusVal = view.GetFocusedValue();
string strSequence = "";
if (FocusVal != null)
strSequence = FocusVal.ToString();Alternatively:
string strSequence = (view.GetFocusedValue() != null) ? view.GetFocusedValue().ToString() : "";
Would call
GetFocusedValue
twice though so only use if that's ok. Or ifGetFocusedValue
returns a string:string strSequence = view.GetFocusValue() ?? "";
edit: oh I missed the DBNull thing, well that might change things in ways unknown to me - I've never used database code.. :(
-
string.Empty doesn't change the value of the string unless you assign it, and the OP was doing an equality test. What do you think it does? If his test is
if (value == string.Empty)
, this doesn't changevalue
."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Pete O'Hanlon wrote:
string.Empty doesn't change the value of the string unless you assign
You are absolutely right, and I grovel, lots. :-O I can only blame complete and utter brain failure, brought on by unexpected sunshine.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones