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. if...else if.. & bad casting

if...else if.. & bad casting

Scheduled Pinned Locked Moved The Weird and The Wonderful
cssquestionannouncementcareer
3 Posts 2 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.
  • R Offline
    R Offline
    Robin Imrie
    wrote on last edited by
    #1

    I recently started a new job and my first project it to do a maintenance release of one of the companies products. Yesterday I found code like this...

    if ((CString)presuffixS.GetAt(i) == "%") { // Add ID, TYPE, FORMAT
    if ((CString)presuffixS.GetAt(i+1) == "i") {
    presuffix = presuffix + szDeviceID;
    i++;
    }
    else if ((CString)presuffixS.GetAt(i+1) == "t") {
    presuffix = presuffix + szType;
    i++;
    }
    else if ((CString)presuffixS.GetAt(i+1) == "n") {
    presuffix = presuffix + szId;
    i++;
    }

    // and so it continues for and other 6 if elses
    

    }

    Could it be any less efficient? I also found this technique used several other places too!

    Thanks, Robin.

    P 1 Reply Last reply
    0
    • R Robin Imrie

      I recently started a new job and my first project it to do a maintenance release of one of the companies products. Yesterday I found code like this...

      if ((CString)presuffixS.GetAt(i) == "%") { // Add ID, TYPE, FORMAT
      if ((CString)presuffixS.GetAt(i+1) == "i") {
      presuffix = presuffix + szDeviceID;
      i++;
      }
      else if ((CString)presuffixS.GetAt(i+1) == "t") {
      presuffix = presuffix + szType;
      i++;
      }
      else if ((CString)presuffixS.GetAt(i+1) == "n") {
      presuffix = presuffix + szId;
      i++;
      }

      // and so it continues for and other 6 if elses
      

      }

      Could it be any less efficient? I also found this technique used several other places too!

      Thanks, Robin.

      P Offline
      P Offline
      Power Puff Boy
      wrote on last edited by
      #2

      It could. You could, in addition, convert the CString to a std::sting, then copy everything to a char* which has previously been allocated with malloc, and allocate it in the loop, but don't use free. Before you do that translate everything to Klingon.

      If you're having a bad day, stare at your feet and smile.

      R 1 Reply Last reply
      0
      • P Power Puff Boy

        It could. You could, in addition, convert the CString to a std::sting, then copy everything to a char* which has previously been allocated with malloc, and allocate it in the loop, but don't use free. Before you do that translate everything to Klingon.

        If you're having a bad day, stare at your feet and smile.

        R Offline
        R Offline
        Robin Imrie
        wrote on last edited by
        #3

        :thumbsup: :)

        Thanks, Robin.

        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