Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
//-------------------Class 01
public partial class Form1 : Form
{
PackCards_1 packCards_1 = new PackCards_1();
private void card1_MouseClick(object sender, MouseEventArgs e)
        {
            card13 = packCards_1.Card01; //card13 is of type Card
            //I want to transfer the properties from one Card to another Card
            //but nothing is happening
            //where do I fail?
        }
}
//-------------------Class 02
 public class PackCards_1
    {
        public Card Card01 = new Card();
        private void InitializeComponent()
        {
            Card01.point = 1;
            Card01.atack = 1;
            Card01.shield = 1;
            Card01.life = 1;
            Card01.UpdateCard();
        }
    }


What I have tried:

this code I tried. I dont lie.
Posted
Updated 7-Dec-16 5:58am
Comments
Philippe Mori 7-Dec-16 9:58am    
You don't show the definition of Card class. By the way, naming controls with numbers rarely make sense. And where is the question?
_Q12_ 7-Dec-16 10:53am    
public partial class Card : UserControl
{
public Card()
{
InitializeComponent();
}

public int point = 0;
public int atack = 1;
public int shield = 1;
public int life = 2;

}
Richard MacCutchan 7-Dec-16 10:07am    
Where is the constructor for PackCards_1, and where do you call PackCards_1.InitializeComponent?
_Q12_ 7-Dec-16 10:10am    
this is the entire class.
namespace game_CardGame
{
public class PackCards_1
{
public Card Card01 = new Card();
private void InitializeComponent()
{
Card01.point = 1;
Card01.atack = 1;
Card01.shield = 1;
Card01.life = 1;
Card01.UpdateCard();
}

}
}
Richard MacCutchan 7-Dec-16 10:36am    
I repeate: where is the constructor for PackCards_1, and where do you call PackCards_1.InitializeComponent?

 
Share this answer
 
Comments
_Q12_ 7-Dec-16 10:36am    
very interesting!
but it didn't work for me...
I did this:

public partial class Card : UserControl
{
public Card()
{
InitializeComponent();
}

public int point = 0;
public int atack = 1;
public int shield = 1;
public int life = 2;

public Card ShallowCopy()
{
return (Card)this.MemberwiseClone();
}

}

and in Form1 :
PackCards_1 packCards_1 = new PackCards_1();
private void card1_MouseClick(object sender, MouseEventArgs e)
{
card13 = packCards_1.Card01.ShallowCopy();
}

I think i did ok as your example suggest it, but the same result as before - nothing happens.
card13 = new Card();
card13.point = packCards_1.Card01.point;
card13.atack = packCards_1.Card01.atack;
// do the same for the rest of the properties
 
Share this answer
 
Comments
_Q12_ 7-Dec-16 10:01am    
card13 = packCards_1.Card01; //-- i mean it is getting all the properties from card01 without retyping them all.
I did the new card13 = new Card();, but is the same as before - no change.
F-ES Sitecore 7-Dec-16 10:11am    
When you do

card13 = packCards_1.Card01

you're not copying the properties across, you're making "card13" point to the same object in memory that "packCards_1.Card01" is point at, so both variables reference the same object so if you make a change to card13.point you'll see that packCards_1.Card01 also has its point property updated. Google for "value variables vs reference variables" for articles that go into this deeper. If you want to actually copy the object you need to copy the properties one by one as above, or use something like Clone or MemberwiseClone to make a copy of the object. Simply assigning the variable to something else does not make a copy.
_Q12_ 7-Dec-16 10:41am    
yes, you are right.. only properties are copying between them ... All I want is to get the entire "package" of properties from one object and dump it into another object. Or an alternative !!! the idea is to keep the long text into 1 file and in my main file only to point this get all the stuff from that and in 1 line. Simple, visible and clear. Can you think on other ways? Thank you very much.
_Q12_ 7-Dec-16 11:08am    
heh, this is not working also ...
I did:
card13.point = packCards_1.Card01.point;
card13.atack = packCards_1.Card01.atack;
card13.shield = packCards_1.Card01.shield;
card13.life = packCards_1.Card01.life;
and is giving me the default values from inside the Card Object.
It seems my Card01 is not changing the values or I can not reach the Card01 somehow... It is because are 3 classes?... any thoughts?
Philippe Mori 7-Dec-16 11:17am    
It is not very clear how those 3 classes relate to each other. The above code modify the variable card13 so that it is a distinct object than the one inside packCards_1 but with the same values (at the time they were copied).

I would recommend you to learn more deeply the C# langage and good design/coding practice before writing a lot of code...
You need to write a method in the Card object to do what you want.

Call it Clone (or something appropriate)

C#
public void Clone(Card item)
{
    // either set the fields one at a time, or use reflection to get the PropertyInfo[] 
    // for all of the properties in the Card object, and loop through that array, 
    // calling the _set method for each property. (I would probably choose the 
    // reflection approach if it was me, since classes are subject to change frequently 
    // during development.) 
}


You might also want to create a constructor overload that accepts a Card object as a parameter, and then you can call the Clone method from there if you're creating new Card instances with existing Card objects.
 
Share this answer
 

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