2009-10-12 45 views
2

這個代碼在按下按鈕後被執行。c#對象被奇怪地刪除了

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using xnaWindow.FormUI; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace xnaWindow.MathClass 
{  
    public class Math_Vector 
    { 
     private Vector3 vectorA; 
     private Vector3 vectorB; 
     private Vector3 vectorR; 
     private List<float> vResult; 

     VertexPositionColor[] verts1,verts2,verts3; 

     public void calculate(List<float>v1,List<float>v2) 
     { 
      Console.WriteLine("calculating.."); 

      vResult = new List<float>(); 
      vectorA = new Vector3(v1.ElementAt(0), v1.ElementAt(1), v1.ElementAt(2)); 
      vectorB = new Vector3(v2.ElementAt(0), v2.ElementAt(1), v2.ElementAt(2)); 

      //this is the manual calculation of vector addition 
      float xRes = v1.ElementAt(0) + v2.ElementAt(0); 
      float yRes = v1.ElementAt(1) + v2.ElementAt(1); 
      float zRes = v1.ElementAt(2) + v2.ElementAt(2); 

      vectorR = new Vector3(xRes,yRes,zRes); 
      //vectorR = vectorA + vectorB; 

      verts1 = new VertexPositionColor[2]; 
      verts1[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Black); 
      verts1[1] = new VertexPositionColor(vectorA, Color.Black); 

      verts2 = new VertexPositionColor[2]; 
      verts2[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Black); 
      verts2[1] = new VertexPositionColor(vectorB, Color.Black); 

      verts3 = new VertexPositionColor[2]; 
      verts3[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Black); 
      verts3[1] = new VertexPositionColor(vectorR, Color.Black); 

      int i = 0; 
      //this is for console debug 
      foreach (float va in v1) 
      { 
       Console.WriteLine("adding " + va.ToString() + v2.ElementAt(i).ToString()); 
       vResult.Add(va+v2.ElementAt(i)); 
       i++; 
      } 

     } 

     public Vector3 getV1(){return vectorA;} 
     public Vector3 getV2(){return vectorB;} 
     public Vector3 getV3(){return vectorR;} 

     public VertexPositionColor[] getVertex1() 
     { 
      return verts1; 
     } 
     public VertexPositionColor[] getVertex2() 
     { 
      return verts2; 
     } 
     public VertexPositionColor[] getVertex3() 
     { 
      return verts3; 
     } 
    } 
} 

奇怪的是,verts1,vertes2,verts3在退出函數後總是被忽略。 所以我執行後調用的getters方法總是返回null。

我該怎麼做傢伙?

這是我的呼籲干將

math.calculate(v1, v2); 
verts1 = math.getVertex1(); 
verts2 = math.getVertex2(); 
verts3 = math.getVertex3(); 
+0

你是否調用了你調用的同一個實例的getter? – Guffa 2009-10-12 00:17:18

+0

是的,我願意。我將在這篇文章後張貼我的電話。 你知道如何不讓編譯器刪除對象嗎?像protectThisVariable(verts1); ??? – r4ccoon 2009-10-12 00:22:46

+0

@ r4ccoon如果你的對象存儲在成員變量中,所有其他事物都是相等的,它們不應該丟失它們的值。 – 2009-10-12 00:25:55

回答

0

擺弄了3天的代碼後,我現在知道問題是什麼。 如果您下載我的代碼,並沒有注意到它,

顯然程序混淆了哪些文本框他們需要計算,哪個按鈕觸發什麼。那是由列表中的控件引起的<>

在form_ui中有一些方法將一些控件添加到列表中<>。 我已經把代碼從容器中刪除列表的內容。但它仍然沒有刪除裏面。 !!!!!!!!!!!!!!! XDXP

所以我必須把清除()列表中。那麼裏面的一切都消失了。

現在問題沒有了。

1

如果我猜我敢肯定你正在使用的替代類結構。最有可能的是,您只是像處理類忘記它們是按值複製一樣對結構實例進行處理。所以你可能會在某個地方得到一個未初始化的東西(你看到的是「NULL」)。

+0

+1,聽起來像是任何人想要得到的最合理的結論,直到你可以分享更多的代碼。 – 2009-10-12 20:12:49

+0

nope。這是一個班級。並沒有一個是結構。 我有結構之前的問題,但我已經改變他們類。 – r4ccoon 2009-10-13 01:00:32

+0

這裏是我的項目的鏈接。 http://www.mediafire.com/?sharekey=ff6f03bd91e7612bab1eab3e9fa335ca0b62c612f301a16b – r4ccoon 2009-10-13 01:25:59