Problem with multiple calls to Activator.CreateInstance

Q

quintin.stone

I haven't been able to find any records of this problem no matter how
much I search. I've got a C# app (.NET 1.1.4322) that has multiple
calls to Activator.CreateInstance() in order to create objects that
get added to an array. However, every call after the first encounters
the same problem: the objects already in the array are mutated to
reflect the properties of the class passed to the most recent
CreateInstance call. It's as if CreateInstance returns a reference to
the identical memory location for each and every call, simply
overwriting whatever object was already there.

Has anyone encountered this behavior before? Is there any
documentation of it out there anywhere?
 
N

Nicholas Paldino [.NET/C# MVP]

This is most definitely not the case. More likely than not, you are not
assigning to the array properly, either because you are not incrementing the
index correctly, or you are not properly updating the reference that
ultimately gets assigned to the array. My guess is on the latter.

Of course, if you posted your code and formed a complete example, it
would be easier to point out.
 
Q

quintin.stone

This is most definitely not the case. More likely than not, you are not
assigning to the array properly, either because you are not incrementing the
index correctly, or you are not properly updating the reference that
ultimately gets assigned to the array. My guess is on the latter.

Of course, if you posted your code and formed a complete example, it
would be easier to point out.

Sorry I wasn't more specific, it's an ArrayList, not a true array.
Here's an example of code that exhibits the problem in question:

ArrayList testArray = new ArrayList();
GameBuilding gb =
(GameBuilding)Activator.CreateInstance(typeof(BuildingFirePit));
testArray.Add(gb);
GameBuilding gb2 =
(GameBuilding)Activator.CreateInstance(typeof(BuildingSaltMine));
testArray.Add(gb2);

The arraylist still reports the class type of the first entry as
BuildingFirePit, but when examined, all of its fields have now been
changed so that they match the values set in BuildingSaltMine's
constructor.
 
N

Nicholas Paldino [.NET/C# MVP]

The following program (which has your code in it, and some stub types
that I made) shows that the code that you posted is not the cause of what
you are seeing, but rather, some other code in your program.

You will have to show a complete example which exhibits the behavior you
specified in your OP.
 
Q

quintin.stone

The following program (which has your code in it, and some stub types
that I made) shows that the code that you posted is not the cause of what
you are seeing, but rather, some other code in your program.

You will have to show a complete example which exhibits the behavior you
specified in your OP.

You're right, thanks. You prompted me to go over the base class and I
discovered that someone had set several of the fields to be static and
I hadn't noticed. That was the problem I've been running into.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top