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. Re-using variables can be dangerous

Re-using variables can be dangerous

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpdatabaselinqsysadminxml
4 Posts 3 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.
  • K Offline
    K Offline
    kmoorevs
    wrote on last edited by
    #1

    Re-using variables can be dangerous. The following code had me baffled for hours. This code queries a dozen different XML files on the server, builds a response, and serves it to different dashboards.

            fileName = "FinSummaryByMonth";
            xmlFile = Server.MapPath("Data/" + DistrictNumber + "/" + sYearFolder + "/" + fileName + ".xml");
            srcDoc = XDocument.Load(xmlFile);
            //linq to xml process 1...should build 13 rows with 12 elements...OK
            //append to the buildDoc and move on to the next section
    
            fileName = "MealSummaryByMonth";
            srcDoc = XDocument.Load(xmlFile);
            //linq to xml process 1...should build 11 rows with 12 elements...WRONG! returning 13 rows!
    

    Being a newbie to linq, I had assumed the mistake was in the query. 2 hours later, looking at the output, I realize that the rows in the problem section were identical to the rows in the previous section nevermind that the results should have been ints. :doh:

    "Go forth into the source" - Neal Morse

    P 1 Reply Last reply
    0
    • K kmoorevs

      Re-using variables can be dangerous. The following code had me baffled for hours. This code queries a dozen different XML files on the server, builds a response, and serves it to different dashboards.

              fileName = "FinSummaryByMonth";
              xmlFile = Server.MapPath("Data/" + DistrictNumber + "/" + sYearFolder + "/" + fileName + ".xml");
              srcDoc = XDocument.Load(xmlFile);
              //linq to xml process 1...should build 13 rows with 12 elements...OK
              //append to the buildDoc and move on to the next section
      
              fileName = "MealSummaryByMonth";
              srcDoc = XDocument.Load(xmlFile);
              //linq to xml process 1...should build 11 rows with 12 elements...WRONG! returning 13 rows!
      

      Being a newbie to linq, I had assumed the mistake was in the query. 2 hours later, looking at the output, I realize that the rows in the problem section were identical to the rows in the previous section nevermind that the results should have been ints. :doh:

      "Go forth into the source" - Neal Morse

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

      "Omit needless local variables." -- Strunk... had he taught programming Or, better, write a method and eliminate the repetitive code.

      You'll never get very far if all you do is follow instructions.

      R 1 Reply Last reply
      0
      • P PIEBALDconsult

        "Omit needless local variables." -- Strunk... had he taught programming Or, better, write a method and eliminate the repetitive code.

        You'll never get very far if all you do is follow instructions.

        R Offline
        R Offline
        robocodeboy
        wrote on last edited by
        #3

        PIEBALDconsult wrote:

        Or, better, write a method and eliminate the repetitive code.

        This.

        K 1 Reply Last reply
        0
        • R robocodeboy

          PIEBALDconsult wrote:

          Or, better, write a method and eliminate the repetitive code.

          This.

          K Offline
          K Offline
          kmoorevs
          wrote on last edited by
          #4

          Good advice! I suppose I could stick the file names into a list with any parameters distinct to each query and return a loaded XDocument or Array. Maybe in the next version! Thanks! :)

          "Go forth into the source" - Neal Morse

          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