Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Inline If problem

Inline If problem

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkB123
    wrote on last edited by
    #1

    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

    P L 3 Replies Last reply
    0
    • M MarkB123

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      My blog | My articles | MoXAML PowerToys | Onyx

      M 1 Reply Last reply
      0
      • M MarkB123

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        .IsNullOrEmpty is a method change to .Empty

        OriginalGriffO 1 Reply Last reply
        0
        • L Lost User

          .IsNullOrEmpty is a method change to .Empty

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          P 1 Reply Last reply
          0
          • M MarkB123

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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 by GetFocusedValue is null: NullReferenceException Or the value returned by ToString is null: strSequence is null ToString 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!

            M 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              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

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              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 change value.

              "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.

              My blog | My articles | MoXAML PowerToys | Onyx

              L OriginalGriffO 2 Replies Last reply
              0
              • P Pete OHanlon

                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 change value.

                "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.

                My blog | My articles | MoXAML PowerToys | Onyx

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                ms fxcop says: dont use : (value == string.Empty) use: (value.Length == 0)

                P 1 Reply Last reply
                0
                • P Pete OHanlon

                  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.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  M Offline
                  M Offline
                  MarkB123
                  wrote on last edited by
                  #8

                  Perfect - Many Thanks.

                  1 Reply Last reply
                  0
                  • L Lost User

                    ms fxcop says: dont use : (value == string.Empty) use: (value.Length == 0)

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    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 case value.Length would throw an ArgumentNullException.

                    "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.

                    My blog | My articles | MoXAML PowerToys | Onyx

                    1 Reply Last reply
                    0
                    • L Lost User

                      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 by GetFocusedValue is null: NullReferenceException Or the value returned by ToString is null: strSequence is null ToString 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!

                      M Offline
                      M Offline
                      MarkB123
                      wrote on last edited by
                      #10

                      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

                      L 1 Reply Last reply
                      0
                      • M MarkB123

                        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

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        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 if GetFocusedValue 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.. :(

                        1 Reply Last reply
                        0
                        • P Pete OHanlon

                          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 change value.

                          "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.

                          My blog | My articles | MoXAML PowerToys | Onyx

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #12

                          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

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups