Need Help getting around memory pointing.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MZimmerman6
    New Member
    • May 2010
    • 8

    Need Help getting around memory pointing.

    I am trying to write a program that reads through a word document, examines tables, copies their contents to the clipboard and saves them into lists of lists:
    List<List<Strin g>> and also one List<List<List< String>>>

    Think of it kind of like filing intructions into a list of how to build a dining set or something. The set has different parts, which each will have their own instructions.

    Dining Set
    - Table
    - Step 1 - put on legs
    - Step 2 - place top on legs
    - Chairs
    - Step 1 - unpack cushion
    - Step 2 - assemble back

    and so on.. Where each step is a String.

    I can get each step string and add it to a list of each part, but when I start on a new part, it overwrites the previous part.

    So my instructions for say the table, will be output as the steps for the chairs. It basically makes everything equal to whatever the last part was. This I am assuming is because of memory pointing. Because I go and change stuff that other things reference, it it will change whatever it referencing it.

    So my question is, Is there any way of getting around this? I was hoping that c# had an eval function that I could use in place of this, but none exist.

    I feel like this may be a simple fix, but I just can not think of it currently.


    Sample Output. Note: Each Case shoudl have differnet numbers of steps

    Scenario 1
    Case 1: 7 steps
    Case 2: 7 steps
    Case 3: 7 steps
    Case 4: 7 steps
    Case 5: 7 steps
    Case 6: 7 steps
    Case 7: 7 steps
    Case 8: 7 steps
    Case 9: 7 steps
    Case 10: 7 steps
    Case 11: 7 steps
    Case 12: 7 steps
    Scenario 2
    Case 1: 12 steps
    Case 2: 12 steps
    Case 3: 12 steps
    Case 4: 12 steps
    Case 5: 12 steps
    Case 6: 12 steps
    Case 7: 12 steps
    Case 8: 12 steps
    Case 9: 12 steps
    Case 10: 12 steps
    Scenario 3
    Case 1: 1 steps
    Case 2: 1 steps
    Case 3: 1 steps
    Case 4: 1 steps
    Case 5: 1 steps
    Case 6: 1 steps
    Case 7: 1 steps
    Case 8: 1 steps
    Case 9: 1 steps
    Case 10: 1 steps
    Case 11: 1 steps
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Without seeing your logic for adding steps to a relevant instruction it is hard to say what the problem actually is. If I were trying to solve this problem I would not use List<List<List< String>>>. This makes it very difficult to see where you are actually having a problem.

    I would change this to a first class concept.

    class Item
    {

    public void addSubItem(Item item)
    {
    }
    public void addInstruction( String instruction)
    {
    }
    private string name;
    private List<Item> items; //the subitems that need to be constructed
    private List<String> instructions; //the instructions for this item
    }

    So the Dining Set would contain in its list of items a Table and Chair.

    You would have to write your more method using this concept, but it will be clearer to see what is going on.

    Comment

    • MZimmerman6
      New Member
      • May 2010
      • 8

      #3
      Originally posted by hype261
      Without seeing your logic for adding steps to a relevant instruction it is hard to say what the problem actually is. If I were trying to solve this problem I would not use List<List<List< String>>>. This makes it very difficult to see where you are actually having a problem.

      I would change this to a first class concept.

      class Item
      {

      public void addSubItem(Item item)
      {
      }
      public void addInstruction( String instruction)
      {
      }
      private string name;
      private List<Item> items; //the subitems that need to be constructed
      private List<String> instructions; //the instructions for this item
      }

      So the Dining Set would contain in its list of items a Table and Chair.

      You would have to write your more method using this concept, but it will be clearer to see what is going on.
      unfortunately I was told to use List<List<List< String>>> by the person who I amd doing this for. I am not particularly sure why, but ehh whatever.

      When adding steps, I have a List like

      Code:
      List<String> steps = new List<String>();
      List<List<String>> cases = new List<List<String>>();
      
      steps.add("case 1: Some string");
      steps.add("case 1: Some other string");
      
      cases.add(steps);
      steps.Clear();
      
      steps.add("case 2: Some string");
      steps.add("case 2: Some other string");
      cases.add(steps);
      I clear out the steps list because case 2 has different steps from case 1.

      That is how I am working that, and the problem is that case 1 will end up having the same steps as case 2. it overwrites them or changes the reference or something. Any suggestions with that?

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        Originally posted by MZimmerman6
        unfortunately I was told to use List<List<List< String>>> by the person who I amd doing this for. I am not particularly sure why, but ehh whatever.

        When adding steps, I have a List like

        Code:
        List<String> steps = new List<String>();
        List<List<String>> cases = new List<List<String>>();
        
        steps.add("case 1: Some string");
        steps.add("case 1: Some other string");
        
        cases.add(steps);
        steps.Clear();
        
        steps.add("case 2: Some string");
        steps.add("case 2: Some other string");
        cases.add(steps);
        I clear out the steps list because case 2 has different steps from case 1.

        That is how I am working that, and the problem is that case 1 will end up having the same steps as case 2. it overwrites them or changes the reference or something. Any suggestions with that?
        Basically your problem is what you believed.

        When you add steps to to cases on line 7 you are just doing a shallow copy on to the list. That is you are just copy the address of the steps on the heap. So any modification you do to steps from this point on will affect the one stored in cases.

        To get around this you need to make every step its own object by doing
        List<String> steps = new List<String>();

        I would suggest rolling the addition of steps into its own method

        Comment

        • MZimmerman6
          New Member
          • May 2010
          • 8

          #5
          Originally posted by hype261
          Basically your problem is what you believed.

          When you add steps to to cases on line 7 you are just doing a shallow copy on to the list. That is you are just copy the address of the steps on the heap. So any modification you do to steps from this point on will affect the one stored in cases.

          To get around this you need to make every step its own object by doing
          List<String> steps = new List<String>();

          I would suggest rolling the addition of steps into its own method
          Oh it already is its own method, do not worry, I just wrote a simplified version of what I am doing because there is a lot more.

          But i am unsure exactly what you mean by making each step its own object.

          is there any way to make a more "deep" copy of the contents. say copy the contents rather than the reference?

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            Originally posted by MZimmerman6
            Oh it already is its own method, do not worry, I just wrote a simplified version of what I am doing because there is a lot more.

            But i am unsure exactly what you mean by making each step its own object.

            is there any way to make a more "deep" copy of the contents. say copy the contents rather than the reference?
            When you do the following declaration.

            Code:
            List<String> steps = new List<String>();
            What you are doing is creating an object of type List<String> on the heap. The variable steps just contains the address of that object that is on the heap.

            So if your steps are in its own method.

            Code:
            public List<String> addStep()
            {
              List<String> steps = new List<String>();
            <----- get the data for the steps
            return steps;
            }
            So to use this all you would have to do is
            Code:
            cases.add(addStep());

            Comment

            • MZimmerman6
              New Member
              • May 2010
              • 8

              #7
              Originally posted by hype261
              When you do the following declaration.

              Code:
              List<String> steps = new List<String>();
              What you are doing is creating an object of type List<String> on the heap. The variable steps just contains the address of that object that is on the heap.

              So if your steps are in its own method.

              Code:
              public List<String> addStep()
              {
                List<String> steps = new List<String>();
              <----- get the data for the steps
              return steps;
              }
              So to use this all you would have to do is
              Code:
              cases.add(addStep());
              I had tried something very similar to that before hand thinking that a method would break the pointer issue, much to no success. I was/am still having the same issue.

              Comment

              • hype261
                New Member
                • Apr 2010
                • 207

                #8
                Originally posted by MZimmerman6
                I had tried something very similar to that before hand thinking that a method would break the pointer issue, much to no success. I was/am still having the same issue.
                Could you post the method you are using to add a step to the cases?

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  Since each thing is a subnode of the parent thing...
                  I would use a treeview and make each thing a treenode.
                  Your professor can live with the logic of what you are doing, so long as you make a function in the end that outputs to List<string> objects.

                  Comment

                  • MZimmerman6
                    New Member
                    • May 2010
                    • 8

                    #10
                    Originally posted by tlhintoq
                    Since each thing is a subnode of the parent thing...
                    I would use a treeview and make each thing a treenode.
                    Your professor can live with the logic of what you are doing, so long as you make a function in the end that outputs to List<string> objects.
                    I got it working now. I ended up redoing the part where I add cases, declaring a new list each time, and it worked.

                    Note: Not for school, For work :]

                    Thanks for all of your help though!

                    Comment

                    Working...