I have List-control of objects.
I want find a certain object and save it away for later use.
To do this, I used a foreach-loop and tried to copy the object I found into a new object-variable.
Something like:
Book temporary = null; //Reference to Book-object
foreach (Book B in BookList)
if (txtBox1.Text.Equals(B.ID.ToString()))
temporary = B;
But that doesn´t seem to work.
How can I do this?
Loading

VulpesRank #55 · 29.2K reputationPosted Apr 12, 2012, 5:00 AM EDT
if (txtBox1.Text.Equals(B.ID.ToString()))
Notice that for the Equals() method to work, txtBox1.Text and B.ID.ToString() must be identical. In particular, capitalization should be the same and so should spacing.
VulpesRank #55 · 29.2K reputationPosted Apr 11, 2012, 8:39 AM EDT
So, if you were to change the properties of this object via the temporary variable, it would also change the properties of the Book in the List, as they're the same object.
If you want to create an independent copy of the object (i.e. clone it), then you need to write code to do that in the Book class itself.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 11, 2012, 11:52 AM EDT
Lars Persson91 reputationPosted Apr 12, 2012, 3:48 AM EDT
I create a Book-object which points to null;
I then go through the List to find the book that corresponds to what ever I write in txtBox1.
When I found the right object I want to save it (or rather the referens to it I suppose) so I can use it.
When I do this it seems like "temporary" still points to null.
So I guess that getting the reference to the right Book-object is what I want. Not make a clone of it.
What I'm trying to do is find a Book-object in the BookList, find a Customer-object in the CustomerList and then register the loan.
Lars Persson91 reputationPosted Apr 12, 2012, 6:34 AM EDT
Without the condition it works.
Thanks!