c#对象被奇怪地删除了

c#对象被奇怪地删除了,c#,xna,C#,Xna,此代码在按下按钮后执行 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

此代码在按下按钮后执行

    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;
        }
    }
}

如果我不得不猜测的话,我很确定你使用的是结构而不是类。最有可能的情况是,您只是像对待类一样对待结构实例,而忘记了它们是按值复制的。所以你可能会在某个地方得到一个未初始化的代码(你看到的是“NULL”)。

在修改代码3天后,我现在知道问题出在哪里了。 如果你下载了我的代码却没有注意到

显然,程序不清楚他们需要计算哪个文本框,哪个按钮触发什么。是由列表中的控件引起的

在表单ui中,有一些方法可以将一些控件添加到列表中。 我已经放置了从容器中删除列表内容的代码。但是它仍然没有删除里面的内容!!!!!!!!!!!!!!!XDXP

所以我必须把clear()放在列表中。然后里面的一切都消失了


现在这个问题已经解决了。

在调用calculate的同一个实例上调用getter了吗?是的,我调用了。我会把我的电话贴在这个帖子下面。您知道如何不让编译器删除对象吗?像protectThisVariable(verts1)@r4Coon如果对象存储在成员变量中,所有其他条件都相同,那么它们不应该丢失它们的值。是的,我知道。这就是为什么它很奇怪。呵呵。。也许,如果我能保护variabel不被删除,我现在就可以平静地生活了。仅供参考。我已经使用了断点。单击“计算”按钮后,值就在那里,函数设置了变量。但是在它离开函数后,它的内容就消失了。+1,听起来是这里任何人都会得出的最合理的结论,直到你可以分享更多的代码。不。这是一门课。而且它们都不是struct。我以前在结构上遇到过问题,但我已经将它们改为类。下面是指向我的项目的链接。
math.calculate(v1, v2);
verts1 = math.getVertex1();
verts2 = math.getVertex2();
verts3 = math.getVertex3();