开发者

c# object got deleted strangely

开发者 https://www.devze.com 2022-12-08 11:20 出处:网络
this code get executed after a push of button. using System; using System.Collections.Generic; using System.Linq;

this code get executed after a push of button.

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

the strange thing is, verts1, vertes2,verts3 always get nulled after it exit the function. so the getters method i have called after executing that always return null.

what should i do guys?

this is my call for the getters

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


If I had to guess I'm pretty sure you are using structs instead of classes. And most likely you just treat the struct instances as you would do with classes forgetting that they are copy-by-value. So you likely get an uninitialized one at some place (What you are seeing as "NULL").


after fiddling the codes for 3 days, i now know what the problem are. if you downloaded my code, and havent noticed it,

apparently the program were confused which textbox do they need to calculate, and which button that trigger what. that were caused by controls in a List<>

in the form_ui there are some method to add some controls into the list<>. i have put the code that removes the content of the list from the container. but it still not deleting the inside. !!!!!!!!!!!!!!! XDXP

so i have to put clear() in the List. then everything inside are gone.

now that problem are gone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号