Hi, I'm coding some shit in C#. Sometimes I like to pass array objects between methods. When I do this, I access the passed array as sender from the receiving method. Typically I am passing it to a method in another public class. However, there are not many methods available to the sender object. I would like to have another copy of the array exist where I pass it. I suppose one way to do this would be to use sender.ToString() and then repackage the string into a new array. Are there better ways to do this??? TIA, -b
bung, Your terminology is very confusing. How about some code snippets with comments? If you are passing an array to an objects method, and THAT object wishes to create it's own copy of the array so it can mess with it without messing around with the originally sent copy, then there should be some basic copy methods available to do a shallow copy. (Typically high up in the object hierarchy ) A deep copy would make copies of the objects contained in the array as well, but not sure this is what you want to do. For example...in java I could so something like this: int myArray[]= { 1,2,3,4}; // array of ints master.doSomething(myArray); // NOW inside the doSomething call of the master object: public void doSomething(int[] anArray){ int myCopy[] = anArray.clone(); // now I can mess with my personal copy } Not sure if this answers your question, since I didnt quite understand it peace axeman
instead of doing: int myCopy[] = anArray.clone(); Couldn't you just do: int myCopy[] = anArray; Either way, sounds very simple. -Fast
Well... thats legal, but bung said: I would like to have another copy of the array exist where I pass it. Better yet... if you want to make sure the object which you pass the array to can't screw up your array, it would be better to do it this way: reciever.doSomething(myArray.clone()); instead of trusting the receiver NOT to screw around with your array. peace axeman
heh no problem. thanks for the help. //what i'm finding very confusing right now is how to write code on a tiny little ET reply window. //in class1 i am creating an array list and adding data to it coming from an activeX control. class1 { ArrayList myAL = new ArrayList(); private void button9_Click(object sender, System.EventArgs e) {myAL.Add(e.price); class2.sortAllData( myAL );} //now i am calling a sortAllData method to manipulate the data inside the arraylist. this method belongs to another class. } class2 { private void sortAllData(object sender) {} //what i want to do now in this method is to access a copy of myAL, which i passed to this method from the button9 click event in class1. } PS - i also don't care about what happens to the copy of myAL in class2. help is much appreciated. -b
bung, The object "sender" *IS* the array object you passed, but it has lost its "type". I think what is confusing you, is the fact that you declared it as an object instead of an array in the recieving method. So when the method is called you would have to CAST it back into an array object before you can use it as such. But a BETTER way would be to declare your method correctly so that you are in fact receiving it as an array. For example, instead of: private void sortAllData(object sender) Try declaring it as: private void sortAllData(ArrayList sender) Then do stuff with your "sender" variable just as you would with your myAL variable. peace axeman
I can't believe that was confusing you I was mostly confused by trying to figure out exactly what was confusing you. Confused yet? -Fast
Yeah me too. It's often difficult to understand what a beginning programmer is trying to ask without seeing the source code. I used to be the guy at the UNIX desk debugging everyones code in multiple languages in college (Especially near semester end... what a nightmare ) Many of the languages I didn't even know... but it doesnt matter. ( I only knew about 6 at the time ) I dont even know C#... ha haha... but their all basically the same, with some syntatical sugar changes, so it doesnt matter. peace axeman