473,412 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Method Invoking within a foreach loop causing issues

Ok, here's an interesting one, and any help would be greatly appreciated!

I'm looping through an xml document in order to set properties of an object. I have one property that is actually a Collection of other custom objects. I'm looping through the child nodes and setting the values of these objects. Each time through, the object is populated correctly, however, when I run the "Invoke" on the "Add" method, it sets all objects in the Collection equal to the last object populated. I end up with the correct number of child objects, but they all have the same value as the last one populated.

type is my object type.
obj is the main object to be populated
SetObjectAttributesFromXml is the method being called that loops through the XML attributes. This is working correctly.
propValueType is a Collection type of my base object

Expand|Select|Wrap|Line Numbers
  1.  
  2. object objChild = Activator.CreateInstance(type);
  3. object objList = Activator.CreateInstance(propValueType);
  4. Type objType = objList.GetType();
  5. MethodInfo methInfo = objType.GetMethod("Add");
  6.  
  7. foreach (XmlNode subChild in childNode.ChildNodes)
  8. {
  9.      object objReturnVal = new object();
  10.      objReturnVal = SetObjectAttributesFromXml(objChild, subChild);
  11.             // at this point, objReturnVal is properly populated from my XML
  12.      methInfo.Invoke(objList, new object[] { objReturnVal });
  13.            // first time through, objList shows as a collection with proper values
  14.            // subsequent times through, objList shows as the correct size,
  15.            // however all values are the same as the last processed object
  16.      objReturnVal = null;
  17. }                                
  18.  
  19. propInfoObj.SetValue(obj, objList, null);
  20.  
  21.  
Mar 27 '07 #1
0 710

Sign in to post your reply or Sign up for a free account.

Similar topics

104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
4
by: Ryan Ternier | last post by:
I'm having an issue with an inner loop. Here's the basic Code: For each......{ //Do code here //start other loop For each....{
3
by: Bob | last post by:
I have an abstract class Thing which has a static method Thing GetThing(). Class Something inherits from Thing. SomeThing supplies static info for GetThing to get stuff from the database to create...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
7
by: David Veeneman | last post by:
This is a very simple question, but for moe reason I can't find an answer. Do I take a performance hit if a foreach statement has to evaluate an expression? For example, consider the following:...
5
by: (2b|!2b)==? | last post by:
I get this error: The out parameter 'errMsg' must be assigned to before control leaves the current method when compiling the following code: public void ExecuteCommands(DDLCommands...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.