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
VulpesPosted Apr 12, 2012, 9:00 AM
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.
Lars PerssonPosted Apr 12, 2012, 10:34 AM
Without the condition it works.
Thanks!
Lars PerssonPosted Apr 12, 2012, 7:48 AM
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.
Sam HobbsPosted Apr 11, 2012, 3:52 PM
VulpesPosted Apr 11, 2012, 12:39 PM
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.