Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all ! I'm kind of out of ideas so I need your help in this one.
it's hard to explain the situation but I'l try my best.

I have created a program that creates cars of some types and adds them to a Garage.
I represent the garage with a list. I put values to one instance and add it to the list, then I realize that when I start to add values to a new instance, it changes the values of the instance inside the list.

I'm confused Please help.

What I have tried:

don't really know what to try, I don't really know how it happened
Posted
Updated 27-Jan-17 19:50pm
Comments
Jon McKee 28-Jan-17 1:30am    
Beyond Peter Leow's suggestion which is probably the issue you're having, could you post relevant code? Without seeing the code we can't point you to the exact area the code is modifying the pointer incorrectly.

That is correct, the elements in the list are just references (pointers) to the actual car instances, this is called Passing Reference-Type Parameters (C# Programming Guide)[^]. Usually, we keep an actual artifact (by value) in a locker. For passing by reference, however, you keep information about the actual location (address) of that article on a piece of paper in that locker, this locker is the so-called pointer in C programming. To better grasp the concept, read out pointer[^].
 
Share this answer
 
v2
Quote:
I represent the garage with a list. I put values to one instance and add it to the list, then I realize that when I start to add values to a new instance, it changes the values of the instance inside the list.

Even without code, one can say is that what you think is 2 instances of 2 different things is in fact 2 pointers to same instance, thus the problem.

Along with deeper learning of programming techniques, you should learn debugger.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900