Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# OOP
Hey everyone.
 
I've question to ask.
 
I have this class.
 
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 5-Oct-12 13:27pm
Un_NaMeD1.4K
Comments
Matt T Heffron at 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 at 5-Oct-12 19:18pm
   
You are right. I answered in general way, please see -- it should be enough.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This statement is not true. (This a "politically correct" expression though. Smile | :) )
 
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:
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
  Permalink  
v6
Comments
Un_NaMeD at 5-Oct-12 19:49pm
   
That helped really much. Thank you SA!
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100