0

I encountered a bit strange behaviour that I could not understand I wonder if someone can help me understand it.
The follwing method recives an object array and make some changes in the array members

 public static void adjustRow(object[] row, String column)
        {

            Double price ,units ,invest;
            if (!(Double.TryParse(row[3].ToString(),out price) 
                & Double.TryParse(row[4].ToString(),out units) 
                & Double.TryParse(row[5].ToString(),out invest)))
                return ;
           switch (column)
           {
               case INVEST: row[4] = Math.Round(invest / price,2); break;
               case UNITS:
               case PRICE: row[5]  = Math.Round(units * price,2); break;
           }
        }

the follwing method call the above method:

void editGrids_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {

        DataGridView gridView = sender as DataGridView;
        DataTable source=  null;
        if (gridView != null)source = gridView.DataSource as DataTable;
        if (source != null)
            Systm.adjustRow(source.Rows[e.RowIndex].ItemArray,gridView.Columns [e.ColumnIndex].HeaderText);
    }

I expected that the values in the input array will be changed outside of the first method scope as well but the actual result is that the values remain the same any explanation?
Thanks Eran

2 Answers 2

5

any explanation

Sure, I can think of lots of explanations.

1) gridView is null.

2) gridView.DataSource is not a DataTable

3) One of the calls to TryParse is returning false. (Note: you probably meant to use &&, not &.)

4) Column does not match INVEST, UNITS, or PRICE.

5) The value written into the array is the same value as was already there.

Any of the above would explain your observation.

Sign up to request clarification or add additional context in comments.

5 Comments

I can see the changes in the first method but they are not reflected outside the method scope...
How do you see the changes in the first method? How are they not reflected outside the method?
I added breack poin in the first method and saw that the values are changed as expected in the method scope , I even try to return the array and the returned array was just fine but I cant understant why should I retun it while the values changes should refelct outside the method anyway
Indeed; an array is a collection of variables. Changing the variable should be observable everywhere that has a reference to that array. Therefore: perhaps the arrays you are looking at are not actually the same reference as each other. For example, if you say typeof(string).GetConstructors()[0] = null; Console.WriteLine(typeof(string).GetConstructors()[0] == null); you'll get "false"; the two arrays returned by GetConstructors are two different arrays.
Sounds like from nobugz's answer that this is what is in fact happening.
1

You need to pass array by reference, not by value

http://msdn.microsoft.com/en-us/library/0f66670z%28VS.71%29.aspx

2 Comments

can you please explain why?? didnt the array has refernce to the value he keep in it?? the behaviour I described didnt fit example 4 in your link and that the conflict I cant understand
Unless I'm missing something, the link you gave says that Eran's code should change the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.