CoordA = CoordB; Not work?

Why does my Class data not copy across right?

Page 1 of 1

3 Replies - 1507 Views - Last Post: 07 October 2008 - 05:31 PM Rate Topic: -----

#1 Codersys   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-May 08

CoordA = CoordB; Not work?

Post icon  Posted 07 October 2008 - 02:08 PM

I have a Bignumber class like this...
  class BigNumber
	{
		public const int	MaxUnits = 4;
		public const float  MaxFloat = 1.0f;
		public const int	MaxInt = 1000000;  

		public int sign;
		public float f;
		public int[] n = new int[MaxUnits];

	  ..........
	  .........
	  etc etc
  }



If I create a BigNumber class say CoordA and CoordB, then do this...
CoordB = CoordA;

The data from CoordA does not copy to CoordB correctly!!!
If I use say Vector3 class for CoordA and CoordB then its fine, just not when I use my own data class!!!

Hope this makes sense and that someone will tell me what I`ve done wrong!
Is it this bit of code some how?...
 public int[] n = new int[MaxUnits];



Is This A Good Question/Topic? 0
  • +

Replies To: CoordA = CoordB; Not work?

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7485
  • View blog
  • Posts: 15,514
  • Joined: 16-October 07

Re: CoordA = CoordB; Not work?

Posted 07 October 2008 - 04:06 PM

Let's make a Widget:
class Widget {
	private string name;
	private double grams;
	public Widget(string name, double grams) {
		this.name = name;
		this.grams = grams;
	}
	public string Name { get { return this.name; } }
	public double Grams { 
		get { return this.grams; }
		set { this.grams = value; }
	}
}



And test it:
Widget a = new Widget("Thingy", 12);
Widget b = a;  // nothing is copied, a and now b references the same object
a.Grams = a.Grams * 2;
Writeline(b.Grams); // prints 24, they are the same thing



If wou want to copy an object, make a method that allows the object to copy itself. The two most common methods are to implement ICloneable or use a copy constructor. We'll use the later.

class Widget {
	//..
	public Widget(Widget obj) {
		this.name = obj.name;
		this.grams = obj.grams;
	}
	//..
}



And test it:
Widget a = new Widget("Thingy", 12);
Widget b = new Widget(a);  // new copy
a.Grams = a.Grams * 2;
Writeline(b.Grams); // prints 12, a and b are separate instances



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Codersys   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-May 08

Re: CoordA = CoordB; Not work?

Posted 07 October 2008 - 04:36 PM

:^:
Widget b = a; // nothing is copied, a and now b references the same object

Ah ha I see now why I was struggling... To solve the problem I had done as you suggest and created a Copy Function like this...
	   public void CopyFrom(BigNumber from)
		{
			sign	=   from.sign;
			f = from.f;
			n[0] = from.n[0];
			n[1] = from.n[1];
			n[2] = from.n[2];
			n[3] = from.n[3];
		}


And was using CoordA.CopyFrom(CoordB) to set A same as B values.

I just didn`t realise why A=B was not working! Many thanks indeed for that.

I`m sometimes amazed that I have got as far as I have in my space game considering I don`t seem to know much details about c# :-)
See my game blog here if you like... www.SpaceUnlimited.blogspot.com
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7485
  • View blog
  • Posts: 15,514
  • Joined: 16-October 07

Re: CoordA = CoordB; Not work?

Posted 07 October 2008 - 05:31 PM

View PostCodersys, on 7 Oct, 2008 - 07:36 PM, said:

I just didn`t realise why A=B was not working!


Here's the trick, if A and B were of type int, it would work. Primitives assign normally, objects essentially assign references. In Java you're not allowed to mention the p-word ( pointers ). It's in the fine print somewhere. ;) But there is a pass by reference thing going on.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1