开发者

C# creating a Class, having objects as member variables? I think the objects are garbage collected?

So I have a class that has the following member variables. I have get and set functions for every piece of data in this class.

public class NavigationMesh
{
    public Vector3 node;
    int weight;
    bool isWall;
    bool hasTreasure;

    public NavigationMesh(int x, int y, int z, bool setWall, bool setTreasure)
    {
        //default constructor
        //Console.WriteLine(x + " " + y + " " + z);
        node = new Vector3(x, y, z);

        //Console.WriteLine(node.X + " " + node.Y + " " + node.Z);

        isWall = setWall;
        hasTreasure = setTreasure;

        weight = 1;

    }// end constructor

    public float getX()
    {
        Console.WriteLine(node.X);
        return node.X;
    }
    public float getY()
    {

        Console.WriteLine(node.Y);
        return node.Y;
    }
    public float getZ()
    {
        Console.WriteLine(node.Z);
        return node.Z;
    }

    public bool getWall()
    {
        return isWall;
    }

    public void setWall(bool item)
    {
        isWall = item;
    }

    public bool getTreasure()
    {
        return hasTreasure;
    }


    public void setTreasure(bool item)
    {
        hasTreasure = item;
    }

    public int getWeight()
    {
        return weight;
    }

}// end class

In another class, I have a 2-Dim array that looks like this

NavigationMesh[,] mesh;

mesh = new NavigationMesh[502,502];

I use a double for loop to assign this, my problem is I cannot get the data I need out of the Vector3 node object after I create this object in my array with my "getters".

I've tried making the Vector3 a static variable, however I think i开发者_如何学Ct refers to the last instance of the object. How do I keep all of these object in memory? I think there being garbage collected. Any thoughts?


In C#, don't build get and set functions like that. Use a property:

public float X
{
   get {Console.WriteLine(node.X); return X;}
}

public bool IsWall {get;set;}

If you have a reference to these items, they are not garbage collected.


If you are maintaining a reference to them, they are guaranteed to NOT get garbage collected.

I don't see where you are defining node. Is it possible that another piece of code is interfering with it? Is it declared static? If it is declared static, there is only one instance of it (instead of one per class), and this could be the source of some confusion.

Why is your node field declared public?

I would put that in a property, and set a breakpoint on the set accessor, so that you can see where it is getting modified.


Vector3 is a struct, so it is not an object. It is a value. This means that it is definitely not being garbage collected.

It is a value type, so every time you pass it along a copy is made. Could it be that you get a copy and modify that? It is not clear to me what you mean, when you say you can't get data out of node.


In the class definition everything seems kosher to me except the fact the the node field is declared public - you should avoid this, but I hope you do not assign null to it.
If you do not your Vector object will not be garbage collected.

So what exactly is happening when you try to access the getters? Does GetX throw a null pointer exception?


We cannot see what code you are using to instantiate the 502 by 502 array: Are you sure you are instantiating a new node for every one? If yes, than you should easily be able to retrieve the properties. mesh[x,y].getWall() would give you the correct value for given x's and y's.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜