parameters: struct unexpectedly not passed by value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BeemerBiker
    New Member
    • Jul 2008
    • 87

    parameters: struct unexpectedly not passed by value

    I am getting unexpected behavior in parameter passing, and was wondering if by adding "code" to a "public struct", I have made it behave like a class.

    Code:
            static void SpinOneCard(board tb, ref OneSavedBoard osb, int SrcCol, int SrcCard, int DesCol, int DesCard)
            {
                tb.nSpins = 0;
                osb.moveto(ref tb , SrcCol, SrcCard, DesCol, DesCard);
                osb.init(ref tb, BoardSeries.Count);
                BoardSeries.Add(osb);
            }
    The struct "board" is passed by value and then referenced in the call to .moveto and .init. This works as expected and the cards on the board table reflect the new move. Unexpectedly, the program that calls SpinOneCard also has its board changed.

    Code:
                    {
                        OneSavedBoard osb = new OneSavedBoard();
                        tb.nSpins++;
                        SpinOneCard(tb, ref osb, iColumn, nCard, i, DesLoc);  
                    }
    Setting a breakpoint after the last line, I observe that the struct "tb" has been changed by the .moveto. I did not expect that.

    I started off with a simple struct for "board" but ended up added all sorts of stuff to it.

    Code:
            public struct board
            {
                public column[] ThisColumn;
                public int score;
                public int from;
                public void init()
                {
                    int i;
                    ThisColumn = new column[11];
                    for (i = 0; i < 11; i++)
                        ThisColumn[i].init();
    ..etc.. same for that column[] it also contains a lot of code


    SOLVED - The part I was testing was a reference, everything else was passed by value. SoRRy-mY BAd
    Last edited by BeemerBiker; Aug 25 '10, 02:19 AM. Reason: solved problem
Working...