Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass an object by value?

Status
Not open for further replies.

NeilTrain

Programmer
May 27, 2003
275
US
I know this is probably simple, but I cant seem to find the solution in MSDN.

I have a collection of custom objects. The list of these objects is shown in a listbox on my form. I have a copy button that is supposed to create a new object with all the same data as the selected object. The only difference is the Name property changes to "Copy of " + object.Name. Simple enough, except the new object is a reference of the old, so the original object also gets its name changed to "Copy of ..." as well as its data changed whenever the new object is worked with.

heres my copy method:
Code:
[navy]public void[/navy] CopySurface()
{
	[green]//Creates a new surface with settings of selected surface[/green]
	Surface newSurface = [navy]new[/navy] SurfaceCollection[intCurrentSurface];
	newSurface.Name = "Copy of " + newSurface.Name;
	SurfaceCollection.Add(newSurface);
}
 
oops the "new" keyword doesnt go in there. But other than that the method works, but by reference, not by value as I wouild like it to.
 
Sounds like your class needs to implement ICloneable, which is the .net way to dupe an object.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top