|
Because mydef is a class, all your variables (myobj, myobj2 ) are references to the same object.
budidharma wrote: myobj2 = myobj; // CAN I DO THIS?
You can do that, but now myobj2 and myobj both point to the same object. So
myobj.SetValues(3,4); is the same as myobj2.SetValues(3,4);
If mydef was a struct, and you did myobj2 = myobj; ,
myobj2 was a different object than myobj (someone pleas correct me if i'm wrong). myobj is then sort of copied to myobj2 .
Hope this makes sense
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
That's what I was afraid of. I don't want to set myobj2 to the location in memory of myobj. I want to copy all the data from myobj into myobj2.
I don't want this to be written as structs.
I know I could simply write a copy function:
public static void Copy(mydef myobj, mydef myobj2)
{
myobj2.Set(myobj.Get())
}
But that's a very long winded and very ... not elegant.
What's the better method of doing this in C#?
|
|
|
|
|
The usual/recommended way is to implement IClonable.Clone . What i usually do is something like this:
public class MyDef : IClonable {
...
protected MyDef(MyDef original){
}
public object Clone {
return new MyDef(this)
}
public static MyDef Copy(MyDef original){
return new MyDef(original)
}
...
}
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
-- modified at 12:28 Tuesday 1st November, 2005
|
|
|
|
|
Thank you. Above what you wrote is how I was thinking about doing it.
But now that you've mentioned the ICloneable interface, I'd like to learn that method as well.
I've been searching for a tutorial on this for over an hour, and have not found one. I'm not exactly how to use it.
Can you explain a little bit about this? Thanks.
|
|
|
|
|
|
Awesome, thank you very much.
I also noticed that there is an IComparable interface.
Am I correct in assuming that IComparable is implemented just like ICloneable, with the appropriote changes?
And is it preferable to implement IComparable rather than overloading the == operator?
|
|
|
|
|
You're welcome
You can also implement IComparable is you want, but that interface is normally used for sorting and stuff (according to MSDN[^], the Remarks section). To be honest, i never used IComparable, because i never sorted anything...
So if you don't want to sort, but just want to have a way of telling if the two instances are equal, I would override Equals(obj) . There's also a static overload of Equals, but you don't have to override that, because it just calls the Equals of the first object. And if you want, you can overload/overidde the == operator, and then just call Equals:
public override bool Equals(object obj){
}
public operator ==(object obj){
return this.Equals(obj)
} For more info about Equals, IComparable and sucj, you can read this article: General Guidelines for C# Class Implementation[^]
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
God, I must be really annoying by now ... but I'm having some problem i can't seem to find. I'm sure I'm overlooking something stupid.
... relevant methods from the card class ...
<br />
public Card(Card c)<br />
{<br />
m_Rank = c.GetRank();<br />
m_Suit = c.GetSuit();<br />
}<br />
<br />
public object Clone()<br />
{<br />
return new Card(this);<br />
}<br />
... relevant methods from the deck class ...
<br />
public void Shuffle()<br />
{<br />
Random rand = new Random();<br />
<br />
for (int i = 0; i < 52; i++)<br />
{<br />
int first = rand.Next(52);<br />
int second = rand.Next(52);<br />
<br />
Card tempCard = m_Deck[first].Clone();<br />
m_Deck[first] = m_Deck[second].Clone();<br />
m_Deck[second] = tempCard.Clone();<br />
}<br />
<br />
}<br />
... Throws the error:
Error 4 Cannot implicitly convert type 'object' to 'Poker.Card'. An explicit conversion exists (are you missing a cast?) C:\...\Cards.cs 246 33 Poker
I tried to fix it myself, can't seem to figure out where the problem is.
When I call clone, it creates a new object from this and returns it cast as a card. I thought I'd be able to simply set an object equal to that new object ... apparently not though.
I also tried:
Card c = new mycard.Clone(); ... that throws an error as well.
Sorry for so many questions. Thank you for all the help.
|
|
|
|
|
God, I just figured out what you meant. You said with this method, I need to manually cast each return value, for example:
Card tempCard = (Card)m_Deck[first].Clone();
Apparently it's because Clone returns an object. Screw that! I'm going to use the latter static method that returns a Card!
Thanks again for all the help.
|
|
|
|
|
You're not annoying at all, i'm happy to help!
I'm glad you figured it out yourself. Of course, i can explain (almost) everything for you, but a real programmer can solve problems, i think.
But that doesn't mean you should stop asking questions though!
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
As Marc said, you should implement the ICloneable interface.
Notice that if you have to do a simple shallow copy as in the case you exposed, you don't have to write your own copy code, but you can use the Object.MemberwiseClone method instead. (You'll have to implement ICloneable anyway though, because MemberwiseClone is protected)
Cheers
|
|
|
|
|
|
That's true (and very cool ), but:
1) The object you want to copy must be Serializable
2) I think (not tested) that it's slower than the usual way of Clone
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
Alright, I have an enum called Suit ...
<br />
public enum Suit<br />
{<br />
Hearts = 0,<br />
Diamonds = 1,<br />
Spades = 2,<br />
Clubs = 3,<br />
Null = -1,<br />
}<br />
And I have a function I need to create that will return the correct rank type given an integer.
I have written this function to work with a switch statement:
switch(i)
{
case 0:
return hearts;
...
}
BUT, there must be a much cleaner way to do this, by parsing the enum type from the value - I just don't know the syntax and can't seem to find it.
It must be something like:
Rank r = Enum.Parse(Rank, i);
... of course that doens't work. But it must be something similar.
Any suggestions?
|
|
|
|
|
Its easier than you would expect. Just convert the type:
int i = 0;
Suit suit = (Suit)i; // suit == Suit.Hearts
int index = (int)suit; // index == 0;
|
|
|
|
|
Simply casting to the enum type should work.
Rank r = (Rank)i;
Need to be careful though, this will compile and run even if the value of i is outside the range of Rank.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Thanks! Works perfect! I followed your advice and added error checking to make sure the value is between 0 and NUMRANKS (and 0 AND NUMSUITS for suits).
Thanks again!
|
|
|
|
|
presentation layer passes .data. to business layer.
Business layer validate the .data. if any error exist Alert message should be displayed on screen with option to continue with invalid data. And post the invalid data into a Database table with flag errorfound= .true.
fdfff ff
|
|
|
|
|
DialogResult dr = MessageBox.Show("Your data has errors, Continue?","Error!",MessageBoxButtons.YesNo);
if(dr == DialogResult.Yes)
{
}
HTH
|
|
|
|
|
your code accessing presentation layer code
as long as architecture is concerned business layer should not call any presentation layer code.
do you have any other method
fdfff ff
-- modified at 3:15 Wednesday 2nd November, 2005
|
|
|
|
|
Hello,
I am trying to set up the ODBC Connection. I have the System.Data reference, and I am trying to use System.Data.Odbc directive, but there is no such option as Odbc. Any ideas why?
Thank you.
|
|
|
|
|
|
|
hai, does anyone know how to display a program file eg microsoft document in my window form? thank you
|
|
|
|
|
You could use the Microsoft Word ActiveX object to do this. However, all that's doing is embedding an instance of MS Word into your application. The end user would need MS Word installed before he can run your app. Is this acceptable?
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Morality Apart from God
Judah Himango
|
|
|
|