ASP.net c# magical array issue -
I'm experiencing an interesting problem with the following code: I. ProductObject is a custom object in which one of the product-related variables
"Town A" value: 1
"Town B", which includes 'Vendor Location ID'
Viewing these items in the ListBox: Value: 2
Also given: Both items are selected in the list.
1 Product ObjectRear [] ProductsObjectRe = New ProductsObjects [lstLocation.Items.Count]; 2 int counter = 0; 3 foreach (List Itam Lee in List Location) 4 {5 If (Lee.Achard == True) 6 {7 Product Object. Vendor location id = li.value; 8 product objects around [counter] = product object; 9} 10 counter ++; 11} After executing , the above code returns this result to:
productObjectArray [0] .Vendor location = 2 ProductObject [1] Vendor location = 2 I feel disturbed if I step through the code, Product ObjectRe [0]. Vendor Location ID = 1 and Counter = 1 Line 7. Then the product object is [0]. Vendor Location ID is magically 2 and counter = 1 is equal.
It appears that your "product object" has been declared outside of this code block, and So you are referring to the same object in your element. Therefore, when you change the last time "Product object" in the loop, it affects all the objects in the array, because they all point to the same exact object. What you need to do is the new instance of your object in the array point of each element.
Comments
Post a Comment