How does one pass an array to a method and regurgitate the array in the new method?

Discussion in 'Trading Software' started by bungrider, Nov 25, 2003.

  1. 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???

  2. 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 java I could so something like this:

    int myArray[]= { 1,2,3,4}; // array of ints


    // 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 :confused: :D


  3. instead of doing: int myCopy[] = anArray.clone();

    Couldn't you just do: int myCopy[] = anArray;

    Either way, sounds very simple.

  4. 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:


    instead of trusting the receiver NOT to screw around
    with your array.



  5. :D 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.


    ArrayList myAL = new ArrayList();

    private void button9_Click(object sender, System.EventArgs e)
    class2.sortAllData( myAL );}
    //now i am calling a sortAllData method to manipulate the data inside the arraylist. this method belongs to another class.


    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.
  6. 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.



  7. AHHHH!!

    Thank you, sir!!

  8. I can't believe that was confusing you :D I was mostly confused by trying to figure out exactly what was confusing you. Confused yet? :D

  9. 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 :D
    (Especially near semester end... what a nightmare :D )

    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.



  10. By the way bung,

    my FEE will be some trading ideas... ha ha ha :D


    #10     Nov 25, 2003