Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey everyone.

I've question to ask.

I have this class.

C#
public class Node
{
    public int Kova1;               // Kova 1
    public int Kova2;               // Kova 2
    public int Kova3;               // Kova 3

    public int ActionNo;            // Yapılan İşlem

    public Node(int kova1, int kova2, int kova3, int actionNumber)
    {
        Kova1 = kova1;
        Kova2 = kova2;
        Kova3 = kova3;
        ActionNo = actionNumber;
    }

    public Node(int kova1, int kova2, int kova3)
    {
        Kova1 = kova1;
        Kova2 = kova2;
        Kova3 = kova3;
    }

    public Node()
    {
    }

    public Node AnneNode;
}


Whenever I create an instance of this class and do some operation on that instance, it affects all the other instances.

How can I overcome this?

My best regards..
Posted
Comments
Matt T Heffron 5-Oct-12 19:01pm    
Can you show us the usage where this is failing? I see nothing here that should cause the symptom you're describing.
Sergey Alexandrovich Kryukov 5-Oct-12 19:18pm    
You are right. I answered in general way, please see -- it should be enough.
--SA

1 solution

This statement is not true. (This a "politically correct" expression though. :-))

There is no such thing as "instance overwriting the instance", actually. It's either the same instance or not. There is nothing in your code which would cause any cross-modification, and there is no static code. So, I must conclude, no "overwriting" takes place. It's just your "experiment" is invalid, or your observations, but you did not provide any information.

I seriously suspect you don't understand something very, very, very basic. Do you really understand that the class is the reference type and not a value type? The variable are not "copied" by assignment but reference the same object. Consider:
C#
Node first = new Node();
Node second = new Node();
second.Kova1 = 1; //will not effect first

//but
second = first; // no more old second object anymore; first and second simply reference the same object, so:
second.Kova2 = 2; // first.Kova2 becomes 2, but not because the object "overwrites" anything, but just because there are two references to the SAME object


Please, get back to the very basics.

Also, some problems with the code:

The constructor public Node() {} is not needed at all.

The public declarations may not be needed. If you are going to use some types of members in the same assembly, they should better be internal. This is the same as public, only for the same assembly. You should not allow more access than this is really needed.

In most cases, the public or internal type fields means bad style. For better code maintainability, make all fields private and expose them only throw properties. The properties can have any access modifiers. At first, turn all fields into auto-backed properties, so later you will get an opportunity to write some specific getters or setters for them.

Good luck,
—SA
 
Share this answer
 
v6
Comments
Un_NaMeD 5-Oct-12 19:49pm    
That helped really much. Thank you SA!
Sergey Alexandrovich Kryukov 5-Oct-12 19:50pm    
My pleasure. (I added minor updates to this post.)
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900