locked
public List<Vector3[]> ListPoints = new List<Vector3[]>(); RRS feed

  • Question

  • Why do I have to create a bogus array (MUST USE ToARRAY()), dump all my array data in to the bogus array, change the value I need changed, and then dump the data back into the list instead of updating like I would anything else? You should either fix this problem or throw up an error ....

    The problem is when updating like this... ListPoints[2].SetValue(new Vector3(200,400,200), 4) or ListPoints[5][6] = new Vector3(100,100,200) ...  In all INDEX the subindexes are changed instead of the single index you are pointing at pointing at...

    • Moved by Lie You Friday, August 12, 2011 2:08 AM XNA related (From:Visual C# General)
    Sunday, August 7, 2011 5:15 AM

All replies

  • Hi,

    would you mind showing the Vector3 class? How its made?


    Mitja
    Sunday, August 7, 2011 5:40 AM
  • // Very Simple Class
    
    
    Vector3[] listPointVectors = new Vector3[20]; // This is a sub holder in my class and is never initialized in the class.  
    
    
    public List<Vector3[]> ListPoints = new List<Vector3[]>();// There is a different way of constructing this list and the same results occurred...
    
    public void AddPoints()
    
    {
    
    ListPoints.Add[listPointVectors]; // Add the Vector3 Array to the list
    
    }
    
    // End Class
    
    
    // VerySimpleProgram
    
    // Standard constructor..
    
    VerySimpleClass verySimpleClass;
    
    public VerySimpleProgram()
    
    {
    
    verySimpleClass= new VerySimpleClass();
    
    }
    
    Load()
    
    {
    
    // I search through an array of list points and not just one....
    
    verySimpleClass.AddPoints();
    
    verySimpleClass.AddPoints();
    
    verySimpleClass.AddPoints();
    
    }
    
    
    Update()
    
    {
    
    
    // This should work or throw an error but does neither
    
    ListPoints[2].SetValue(new Vector3(200,400,200), 4) 
    
    // This should also work or throw an error but does neither
    
    ListPoints[5][6] = new Vector3(100,100,200) 
    
    // The problem will be seen easilly in this setup because everything should be zero except for (2,4), (5,6)... That is not what happens!!!
    
    
    
    // Bogus Array but at least it works after wasting a few hours 
    
    Vector3[] bogusArray= new Vector3[20];
    
    bogusArray = verySimpleClass.ListPoints[index].ToArray();
    
    bogusArray[subIndex] = new Vector3(23,54,65);
    
     verySimpleClass.ListPoints[index] = bogusArray; // I figured this out because I tried to use bogusArray.ToList() from here and an error code finally helped...
    
    
    
    }
    
    
    

    Sunday, August 7, 2011 7:01 AM
  • ...

     and Vector3? This isnt a class?


    Mitja
    Sunday, August 7, 2011 7:10 AM
  • ...

     and Vector3? This isnt a class?


    Mitja


    Right Mitja, its a struct. Comes from directX and can be found for instance in XNA-framework:

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector3.aspx

    Regards,

      Thorsten

    Monday, August 8, 2011 7:35 AM
  • ...

     and Vector3? This isnt a class?


    Mitja


    Right Mitja, its a struct. Comes from directX and can be found for instance in XNA-framework:

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector3.aspx

    Regards,

      Thorsten


    If the Vector3 were like Thorsten said, I sueest you to post it to the following catrgory for better support.

    http://forums.create.msdn.com/forums/default.aspx?GroupID=6

    Thanks for your understanding and support.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 9, 2011 5:52 AM
  • You're adding to the list a reference to the same array several times, not adding a copy of it. ListPoints[0] and ListPoints[1] reference the same array. When you change the referenced array, you can see that change from any of its references.

    Change your AddPoints method to this and see the difference:

    public void AddPoints()
    {
        ListPoints.Add(new Vector3[20]); // Adds a new Vector3 Array to the list
    }
    


    The important keyword here is 'new'. You have 1 new object for each time the 'new' keyword is executed.

    • Marked as answer by Lie You Friday, August 12, 2011 2:08 AM
    • Unmarked as answer by Lie You Friday, August 12, 2011 2:08 AM
    Tuesday, August 9, 2011 4:53 PM