Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,543 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,247 people online right now. Registration is fast and FREE... Join Now!




CoordA = CoordB; Not work?

 
Reply to this topicStart new topic

CoordA = CoordB; Not work?, Why does my Class data not copy across right?

Codersys
post 7 Oct, 2008 - 01:08 PM
Post #1


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 12

I have a Bignumber class like this...
CODE

  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?...
CODE

public int[] n = new int[MaxUnits];


User is offlineProfile CardPM

Go to the top of the page


baavgai
post 7 Oct, 2008 - 03:06 PM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,945



Thanked 94 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Let's make a Widget:
csharp

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:
csharp

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.

csharp

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


And test it:
csharp

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.
User is offlineProfile CardPM

Go to the top of the page

Codersys
post 7 Oct, 2008 - 03:36 PM
Post #3


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 12

icon_up.gif
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...
CODE

       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

User is offlineProfile CardPM

Go to the top of the page

baavgai
post 7 Oct, 2008 - 04:31 PM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,945



Thanked 94 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(Codersys @ 7 Oct, 2008 - 07:36 PM) *

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. wink2.gif But there is a pass by reference thing going on.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 01:56AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month