2

consider this scenario

Public static Class GlobalParam
{
//static classes strings int and more...
  class NoneStaticClass
  {
    //some noneStatic params
  }
}

in another class ( none static ) I call for an instance of NoneStaticClass in this fashion

GlobalParam.noneStaticClass NSC = new GlobalParam.noneStaticClass();

//some manipulation on NSC params

Later I use Method Like that

void DoSomething(GlobalParam.noneStaticClass nsc)
{
  GlobalParam.noneStaticClass NewNSC = nsc 
  //Some manipulation in NewNSC 
}

now when I check the data stored in NSC I can clearly that it has been changed , why is that ? does putting none static class inside a static is not correct in some way ?

LordTitiKaka
  • 2,087
  • 2
  • 31
  • 51
  • 1
    I think this question is entirely unrelated to static-ness. This is about references to instances vs. deep copying of instances. – O. R. Mapper Jul 16 '14 at 07:52
  • 1
    Can you make your example more clear? That is, include a simple program that shows the whole flow of logic? It's not entirely clear what you're doing here (e.g. are you passing `NSC` as a parameter to `DoSomething()`? – Kjartan Jul 16 '14 at 07:53
  • 1
    Please explain what you are trying to achieve, as I think the method your trying is not suitable to the problem – 3dd Jul 16 '14 at 07:54
  • 1
    Looks like you're just passing around the same instance of NoneStaticClass. Naming the variable `NewNSC` in that method doesn't make it a new one. – Chris Jul 16 '14 at 07:54
  • ... or [this](http://stackoverflow.com/questions/16696448/how-to-make-a-copy-of-an-object-in-c-sharp) or [this](http://stackoverflow.com/questions/78536/deep-cloning-objects) or [this](http://stackoverflow.com/questions/5843958/how-to-make-a-copy-of-a-reference-type) or [this](http://stackoverflow.com/questions/20698361/cloning-a-reference-type) ... – O. R. Mapper Jul 16 '14 at 07:57
  • Nesting classes is almost never a good idea. And the nesting and static are unrelated to the core of your question. – H H Jul 16 '14 at 07:59
  • 1
    Thank you all and you probably right that it is a dup , but I've never encountered this problem before , and now I can tell it is wrong ! – LordTitiKaka Jul 16 '14 at 08:00

2 Answers2

2

Whatever your class is nested or normal class, whenever you do an assignement like GlobalParam.noneStaticClass NewNSC = nsc, NewNSC and nsc will always reference the same object.

If you want a copy of your nsc object, here are some ways to do it :

  • If your class has all members with value type, you can use a struct instead of class. It will be copied each time you make an assignment with.
  • If not, you should referer to object cloning
Community
  • 1
  • 1
Perfect28
  • 11,089
  • 3
  • 25
  • 45
1

This is because your are not creating a new instance of the class when you write GlobalParam.noneStaticClass NewNSC = nsc you are merely assigning a reference to the existing NSC object to the new variable NewNSC. Therefore when you call NewNSC you are calling the same object as NSC.

In order to create a new instance of the object you would need to do this:

GlobalParam.noneStaticClass NewNSC = new GlobalParam.noneStaticClass();

James
  • 2,195
  • 1
  • 19
  • 22