473,563 Members | 2,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does the instance change?

Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on each
iteration i change the position vector, however when i do this and check my
list it has correctly added each new instance as a separate entry but they
all have the position of the last one to be added. When i steped through it
in debug this was verified, the correct positon details are in on one
iteration and on the next iteration immediately after the new instance has
its position changed so too does the one already in my List.

I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.

Thanks
May 1 '06 #1
18 1742
sb
Although I think I understand what you're saying, I'd like to see the actual
code. Hopefully you can trim things down enough to post a short sample that
demonstrates the problem.

-sb
"Daniel" <Da*****@vestry online.com> wrote in message
news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on
each iteration i change the position vector, however when i do this and
check my list it has correctly added each new instance as a separate entry
but they all have the position of the last one to be added. When i steped
through it in debug this was verified, the correct positon details are in
on one iteration and on the next iteration immediately after the new
instance has its position changed so too does the one already in my List.

I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.

Thanks

May 1 '06 #2
I am not understanding some of your explanation ...

can you also post the definition of "Object" does it by chance have
positionvector declared as a static field ?

Cheers,

Greg
"Daniel" <Da*****@vestry online.com> wrote in message
news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on
each iteration i change the position vector, however when i do this and
check my list it has correctly added each new instance as a separate entry
but they all have the position of the last one to be added. When i steped
through it in debug this was verified, the correct positon details are in
on one iteration and on the next iteration immediately after the new
instance has its position changed so too does the one already in my List.

I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.

Thanks

May 1 '06 #3

Well thats just the point, it doesnt make sense to me either. The Object bit
i gave was just an example but an accurate one, here's a real one:

List<myClass> myList = new List<myClass>() ;

for(int i=0; i<10; i++)
{
myClass myObject = new Object();
myObject.Positi onVector = new Vector3(10 + (i*3),10 + (i*5),10);

myList.Add(myOb ject);
}
And no nothing is static.

So as i said if were to debug and look at what is on myList it goes like
this

on first iteration:

myObject.Positi on = 10,10,10

on second iteration just before the Add myObject.Positi on = 13, 15, 10
then once added, myList now has 2 entries, but both have position 13,15,10.

So my question is, is there something about List that i don't know of to
explain how the first instance put on the list could be effected by the
second?

thanks
"Greg Young" <Dr************ *@hotmail.com> wrote in message
news:u0******** ******@TK2MSFTN GP02.phx.gbl...
I am not understanding some of your explanation ...

can you also post the definition of "Object" does it by chance have
positionvector declared as a static field ?

Cheers,

Greg
"Daniel" <Da*****@vestry online.com> wrote in message
news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on
each iteration i change the position vector, however when i do this and
check my list it has correctly added each new instance as a separate
entry but they all have the position of the last one to be added. When i
steped through it in debug this was verified, the correct positon details
are in on one iteration and on the next iteration immediately after the
new instance has its position changed so too does the one already in my
List.

I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.

Thanks


May 1 '06 #4
"Daniel" <Da*****@vestry online.com> a écrit dans le message de news:
Og************* *@TK2MSFTNGP04. phx.gbl...

| Well thats just the point, it doesnt make sense to me either. The Object
bit
| i gave was just an example but an accurate one, here's a real one:
|
| List<myClass> myList = new List<myClass>() ;
|
| for(int i=0; i<10; i++)
| {
| myClass myObject = new Object();
| myObject.Positi onVector = new Vector3(10 + (i*3),10 + (i*5),10);
|
| myList.Add(myOb ject);
| }
| And no nothing is static.

1. You have not shown the declaration of myClass.
2. Your example instantiates Object into a myClass reference

This code is not complete enough to see if you have declared PositionVector
as static, which is the most likely cause of your problem.

Could you post a *complete* but *brief* and *compiling* example.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 1 '06 #5
Ok you are going to need to be clearer ... this code works fine.

class PositionVector

{

public int x, y, z;

public PositionVector( int _x, int _y, int _z)

{

x = _x;

y = _y;

z = _z;

}

public override string ToString()

{

return this.x.ToString () + ":" + this.y.ToString () + ":" +
this.z.ToString ();

}

}

class myClass

{

public PositionVector PositionVector;

}

class Program

{

static void Main(string[] args)

{

List<myClass> myList = new List<myClass>() ;

for (int i = 0; i < 10; i++)

{

myClass myObject = new myClass();

myObject.Positi onVector = new PositionVector( 10 + (i * 3), 10 + (i * 5),
10);

myList.Add(myOb ject);

}

foreach (myClass c in myList)

{

Console.WriteLi ne(c.PositionVe ctor.ToString() );

}

}

}

"Daniel" <Da*****@vestry online.com> wrote in message
news:Og******** ******@TK2MSFTN GP04.phx.gbl...

Well thats just the point, it doesnt make sense to me either. The Object
bit i gave was just an example but an accurate one, here's a real one:

List<myClass> myList = new List<myClass>() ;

for(int i=0; i<10; i++)
{
myClass myObject = new Object();
myObject.Positi onVector = new Vector3(10 + (i*3),10 + (i*5),10);

myList.Add(myOb ject);
}
And no nothing is static.

So as i said if were to debug and look at what is on myList it goes like
this

on first iteration:

myObject.Positi on = 10,10,10

on second iteration just before the Add myObject.Positi on = 13, 15, 10
then once added, myList now has 2 entries, but both have position
13,15,10.

So my question is, is there something about List that i don't know of to
explain how the first instance put on the list could be effected by the
second?

thanks
"Greg Young" <Dr************ *@hotmail.com> wrote in message
news:u0******** ******@TK2MSFTN GP02.phx.gbl...
I am not understanding some of your explanation ...

can you also post the definition of "Object" does it by chance have
positionvector declared as a static field ?

Cheers,

Greg
"Daniel" <Da*****@vestry online.com> wrote in message
news:Oz******** ******@TK2MSFTN GP05.phx.gbl...
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on
each iteration i change the position vector, however when i do this and
check my list it has correctly added each new instance as a separate
entry but they all have the position of the last one to be added. When i
steped through it in debug this was verified, the correct positon
details are in on one iteration and on the next iteration immediately
after the new instance has its position changed so too does the one
already in my List.

I presume that a new memory allocation isn't being made and so on
setting position it overwrites the same memory my list is looking at but
why? I thought the 'new' operator explicitly tells it to create a new
memory allocation? Am i missing something? Anyone know how i can prevent
this situation.

Thanks



May 1 '06 #6
No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a complete
example that will actually show the problem you are talking about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)

Daniel wrote:
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on each
iteration i change the position vector, however when i do this and check my
list it has correctly added each new instance as a separate entry but they
all have the position of the last one to be added. When i steped through it
in debug this was verified, the correct positon details are in on one
iteration and on the next iteration immediately after the new instance has
its position changed so too does the one already in my List.

I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.

Thanks

May 1 '06 #7
Ok my apologies here is the exact code,

On my external class that calls the code withina loop.

PlayingCard theCardSceneObj = new PlayingCard();

theCardSceneObj = (PlayingCard)_o bjMan.GetObject ((int)ObjectID. Cards);

theCardSceneObj .SetCardFrame(p layer.Hand[j]);

theCardSceneObj .FaceUp = player.Hand[j].ShowState;

theCardSceneObj .IsVisible = true;

theCardSceneObj .Position = new Vector3(seatPos .X + (j * 15), seatPos.Y + (j
* 10), 0);

_objMan.AddToRe nderList(theCar dSceneObj);

Inside the_objMan instance:

class ObjectManager

{

List<SceneObjec t> _sceneObjList;

List<SceneObjec t> __renderList;

public ObjectManager()

{

_sceneObjList = new List<SceneObjec t>();

}

public void AddToRenderList (SceneObject so)
{
_renderList.Add (so);
}

public object GetObject(int ObjectId)

{

int count = 0;

foreach (SceneObject so in _sceneObjList)

{

if (so.ObjectID == ObjectId)

return _sceneObjList[count];

count++;

}

return null;

}
That any clearer?

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:u1******** ******@TK2MSFTN GP03.phx.gbl...
No, sorry, the code you show is *not* an accurate example. If you can't
show the actual code you are using, you have to at least show a complete
example that will actually show the problem you are talking about.

It's near to impossible to locate an error in a code by looking at some
other code that doesn't even have the error in it.

Think of it as going to your local mecanic, show him a picture of your
bike, and ask him why you car is not starting... ;)

Daniel wrote:
Hey guys

I have an instance of an object say:

List<Object> myList = new List<Object>();

Object myObject = new Object();
myObject.Positi onVector = new Vector3(10,10,1 0);

myList.Add(myOb ject);
With the above code as an accurate example i do that in a loop. And on
each iteration i change the position vector, however when i do this and
check my list it has correctly added each new instance as a separate
entry but they all have the position of the last one to be added. When i
steped through it in debug this was verified, the correct positon details
are in on one iteration and on the next iteration immediately after the
new instance has its position changed so too does the one already in my
List.

I presume that a new memory allocation isn't being made and so on setting
position it overwrites the same memory my list is looking at but why? I
thought the 'new' operator explicitly tells it to create a new memory
allocation? Am i missing something? Anyone know how i can prevent this
situation.

Thanks

May 1 '06 #8
Daniel <Da*****@vestry online.com> wrote:
Ok my apologies here is the exact code,
<snip>
That any clearer?


Not a lot. You still haven't provided a short but complete example
which demonstrates the problem. Something we can compile and run.

See http://www.pobox.com/~skeet/csharp/complete.html

(You may well find the problem while trying to come up with a short but
complete example.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 1 '06 #9
This goes through one huge class, which calls another class which is
inherited down from two more. Its impossbile to show a complete example.
Hence my original shortened version that showed only the parts needed to
recreate my problem. Ok i will sort this one out for myself, thanks anyway.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Daniel <Da*****@vestry online.com> wrote:
Ok my apologies here is the exact code,


<snip>
That any clearer?


Not a lot. You still haven't provided a short but complete example
which demonstrates the problem. Something we can compile and run.

See http://www.pobox.com/~skeet/csharp/complete.html

(You may well find the problem while trying to come up with a short but
complete example.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2272
by: delraydog | last post by:
If you've used the Google Gmail spell checker you know that it 'changes' the text area to another type of control that lets them show hyperlinks for the incorrect words. How does Google change the textarea? And what do they change it to? I know it should be pretty simple, I just have no clue because I'm fairly new to DHTML. Any examples would...
1
6830
by: Lance | last post by:
Hi All, I change the current directory in a proc, using CurDir(). The change persists across executions. I would like the CurDir() return value to be the same at the start of each execution. Any suggestions will be appreciated. The code below illustrates the problem. Thanks. Dim currentDir As String MsgBox CurDir()
1
3751
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out minIOPort); bool flag = ThreadPool.SetMinThreads(4, minIOPort); ThreadPool.GetAvailableThreads(out newWorker, out newIOPort); After running this...
6
9138
by: Marc Robitaille | last post by:
Hello, Hello, I developed a UserControl. It has funny behavior. It is composed of three controls. A texbox, a combobox and a button. There are three properties to indicate the visibility of the three controls. The only property which functions well is the one for the button. The two others, even if I do: UserControl.TextboxVisible = True...
2
12065
by: stef mientki | last post by:
hello, Why does Configparser change names to lowercase ? As Python is case sensitive (which btw I don't like at all ;-) but now when really need the casesensitivity, because it handles about names which should be recognized by human, it changes everything to lowercase ???? thanks,
5
1908
by: nma | last post by:
Hi Why the code below does not change the color. I can only see the pointer change only but it does not change the color? Please help and Thnaks nma <td width="100" height="17"><a href="some.html" onmouseover="document.t6.src=Image3.src;" onmouseout="document.t6.src=Image2.src;"><img name="t6" src="Highlight.jpg" width="100"...
2
4147
by: bryanleo | last post by:
We are trying to read data from a microcontroller and interface it through serial port. The output is then displayed in Python using Pyserial or the hyperterminal, the former is more important When you touch the input pins of the microcontroller the value changes real time in hyper terminal. But In the case of the pyserial module, even though...
1
2469
by: bibhukdas | last post by:
Hi All, Just to elaborate on the issue. The requirement is to handle this in javascript 1. User clicks on a button 2. Change the cursor style (We are using document.body.style.cursor="wait") 3. Send an Ajax request and do the processing 4. Once the processing is done change back the cursor to original style (We are using...
1
1761
by: Joe Wyrwas | last post by:
My main program is a Multiple Document Interface (MDI). I create and show 2 or more child forms inside the MDI parent. If I left click on the child forms, the focus and Z-index will change correctly. (E.G. I have form1 and form2 as the child forms, form1 is active. I left click anywhere on form2 and the focus and z-index changes). This works...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.