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. Other Discussions
  3. The Weird and The Wonderful
  4. ToString() and ToUpper() are like, so cool!

ToString() and ToUpper() are like, so cool!

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
11 Posts 9 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.
  • N Offline
    N Offline
    Nathan D Cook
    wrote on last edited by
    #1

    (some meaningless code left out)

    if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
    {
    /*do some stuff*/
    }

    else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
    {
    /*do some stuff*/
    }

    else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
    {
    VoidPayment(combo.SelectedItem.ToString().ToUpper());
    }

    else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
    {
    VoidPayment(combo.SelectedItem.ToString().ToUpper());
    }

    VoidPayment(string paytype)
    {
    if (paytype.ToUpper() == "PAID")
    else if (paytype.ToUpper() == "ADJUSTED")
    }

    I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

    L OriginalGriffO V V N 6 Replies Last reply
    0
    • N Nathan D Cook

      (some meaningless code left out)

      if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
      {
      /*do some stuff*/
      }

      else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
      {
      /*do some stuff*/
      }

      else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
      {
      VoidPayment(combo.SelectedItem.ToString().ToUpper());
      }

      else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
      {
      VoidPayment(combo.SelectedItem.ToString().ToUpper());
      }

      VoidPayment(string paytype)
      {
      if (paytype.ToUpper() == "PAID")
      else if (paytype.ToUpper() == "ADJUSTED")
      }

      I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

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

      Nathan D Cook wrote:

      First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused:

      Absolutely correct. That kind of code comes from people who have no idea what the compiler will be able to make out of this. String operations do not translate to single machine instructions. They usually are loops that go through an array of characters. Only nested loops can slow down the processor even more than having to go through one loop after another. So avoid string operations if you can do the job another way (like using an enum for example) and otherwise try to use them as sparingly as possible.

      Nathan D Cook wrote:

      Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later.

      Here we have a tough choice. You are right, checking and adjusting the string may be redundant here. We might get away with doing it your way. For now. In a year another programmer may have to work on this code and may not know about your assumption that the parameter has already been checked and adjusted. If he calls the function without making sure of that precondition, he will introduce a bug without knowing it. The whole mess could be avoided if you could use a more suitable datatype. One that's not nullable and can be tested cheaply. An enumeration.

      At least artificial intelligence already is superior to natural stupidity

      1 Reply Last reply
      0
      • N Nathan D Cook

        (some meaningless code left out)

        if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
        {
        /*do some stuff*/
        }

        else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
        {
        /*do some stuff*/
        }

        else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
        {
        VoidPayment(combo.SelectedItem.ToString().ToUpper());
        }

        else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
        {
        VoidPayment(combo.SelectedItem.ToString().ToUpper());
        }

        VoidPayment(string paytype)
        {
        if (paytype.ToUpper() == "PAID")
        else if (paytype.ToUpper() == "ADJUSTED")
        }

        I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

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

        To add to CDP1802's response, the if...else if...else part could be nicely replaced with a switch, which gets rid of the need to do it multiple times anyway:

        if (combo.SelectedItem.ToString() != null)
        {
        switch (combo.SelectedItem.ToString().ToUpper())
        {
        case "APPROVED":
        ...
        break;
        case "UNAPPROVED":
        ...
        break;
        ...

        Which is a lot more readable, and may be a lot more efficient when compiled.

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "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

        G 1 Reply Last reply
        0
        • N Nathan D Cook

          (some meaningless code left out)

          if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
          {
          /*do some stuff*/
          }

          else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
          {
          /*do some stuff*/
          }

          else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
          {
          VoidPayment(combo.SelectedItem.ToString().ToUpper());
          }

          else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
          {
          VoidPayment(combo.SelectedItem.ToString().ToUpper());
          }

          VoidPayment(string paytype)
          {
          if (paytype.ToUpper() == "PAID")
          else if (paytype.ToUpper() == "ADJUSTED")
          }

          I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

          V Offline
          V Offline
          VallarasuS
          wrote on last edited by
          #4

          You have a point and I would also suggest to replace the strings with constants to avoid introducing bugs by misspelling them. "APPROVED", "ADJUSTED" and more...

          Regards Vallarasu S | FSharpMe.blogspot.com

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            To add to CDP1802's response, the if...else if...else part could be nicely replaced with a switch, which gets rid of the need to do it multiple times anyway:

            if (combo.SelectedItem.ToString() != null)
            {
            switch (combo.SelectedItem.ToString().ToUpper())
            {
            case "APPROVED":
            ...
            break;
            case "UNAPPROVED":
            ...
            break;
            ...

            Which is a lot more readable, and may be a lot more efficient when compiled.

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            string selection = null;
            if (combo.SelectedItem != null) selection = combo.SelectedItem.ToString().ToUpper();
            switch (selection)
            {
            case "APPROVED":
            //...
            break;
            case "UNAPPROVED":
            //...
            break;
            default:
            //...
            break;
            }

            Software Zen: delete this;

            OriginalGriffO 1 Reply Last reply
            0
            • N Nathan D Cook

              (some meaningless code left out)

              if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
              {
              /*do some stuff*/
              }

              else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
              {
              /*do some stuff*/
              }

              else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
              {
              VoidPayment(combo.SelectedItem.ToString().ToUpper());
              }

              else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
              {
              VoidPayment(combo.SelectedItem.ToString().ToUpper());
              }

              VoidPayment(string paytype)
              {
              if (paytype.ToUpper() == "PAID")
              else if (paytype.ToUpper() == "ADJUSTED")
              }

              I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              in addition: 1: combo.SelectedItem might be null and thus the call to ToString() will die in a less elegant way. I don't event think the ToString to return null if selectedItem is not null. Worst case is will return the object name. 2: Should not use mystring == null but string.isNullOrEmpty(mystring) (unless "" is allowed.) 3: Personal, but I genuinly hate if - else if code. I prefer switch in that case. switch also allows for a fall through so you can pass on combo.SelectedItem.ToString().ToUpper() as is to the VoidPayment where it is, in this case checked again. 4: a nice variable assignment like     string combostringvalue = combo.SelectedItem.ToString().ToUpper() ; would have been nice idd.

              V.

              1 Reply Last reply
              0
              • N Nathan D Cook

                (some meaningless code left out)

                if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
                {
                /*do some stuff*/
                }

                else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
                {
                /*do some stuff*/
                }

                else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
                {
                VoidPayment(combo.SelectedItem.ToString().ToUpper());
                }

                else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
                {
                VoidPayment(combo.SelectedItem.ToString().ToUpper());
                }

                VoidPayment(string paytype)
                {
                if (paytype.ToUpper() == "PAID")
                else if (paytype.ToUpper() == "ADJUSTED")
                }

                I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #7

                Are there a fixed number of values? This sounds like a job for an enumerator[^].


                Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                1 Reply Last reply
                0
                • G Gary R Wheeler

                  string selection = null;
                  if (combo.SelectedItem != null) selection = combo.SelectedItem.ToString().ToUpper();
                  switch (selection)
                  {
                  case "APPROVED":
                  //...
                  break;
                  case "UNAPPROVED":
                  //...
                  break;
                  default:
                  //...
                  break;
                  }

                  Software Zen: delete this;

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

                  Oops! :-O I forgot to take off the ToString when I copy-n-pasted from the OP...

                  Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                  "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

                  G 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Oops! :-O I forgot to take off the ToString when I copy-n-pasted from the OP...

                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    This is a petard I've been hoisted by too many times :-O.

                    Software Zen: delete this;

                    T 1 Reply Last reply
                    0
                    • N Nathan D Cook

                      (some meaningless code left out)

                      if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "APPROVED")
                      {
                      /*do some stuff*/
                      }

                      else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "UNAPPROVED")
                      {
                      /*do some stuff*/
                      }

                      else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "PAID")
                      {
                      VoidPayment(combo.SelectedItem.ToString().ToUpper());
                      }

                      else if (combo.SelectedItem.ToString() != null && combo.SelectedItem.ToString().ToUpper() == "ADJUSTED")
                      {
                      VoidPayment(combo.SelectedItem.ToString().ToUpper());
                      }

                      VoidPayment(string paytype)
                      {
                      if (paytype.ToUpper() == "PAID")
                      else if (paytype.ToUpper() == "ADJUSTED")
                      }

                      I am just a novice programmer, but I saw 3 things that seemed strange to me. First, why check for null every time and a ToString().ToUpper() every time? Why not just check it once and set a string that we can check? :confused: Second, in the paid and adjusted if-else sections, why not just pass PAID or ADJUSTED into VoidPayment? In this particular function, the combo box doesn't change. You already know what section you are in due to the if-else logic. (Or we could pass that same string I mentioned a second ago). Third, in the VoidPayment function why are we doing yet another ToUpper when we know we are passing in either a capitalized PAID or ADJUSTED string? VoidPayment is only called from those two spots, and it's not really a function that should be called elsewhere or added anywhere else later. And they wonder why our code is slow....... Cheers, Nathan

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Definitely a place to use an enumeration.

                      1 Reply Last reply
                      0
                      • G Gary R Wheeler

                        This is a petard I've been hoisted by too many times :-O.

                        Software Zen: delete this;

                        T Offline
                        T Offline
                        TNCaver
                        wrote on last edited by
                        #11

                        Rated 5 for your use of that humorous and archaic phrase. :)

                        If you think 'goto' is evil, try writing an Assembly program without JMP.

                        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