Solved

pass objects

Posted on 2009-04-07
7
176 Views
Last Modified: 2013-12-17
I am passing a  list of objects.
I want to change  values in the list.
IS there any problem with this as it is a little  confusing. Am I changing a copy of the list or the actual list ?


obj1 - create list of objects
-----
 Dim missileList As New List(Of ClassMissile)
  Public Property GetMissiles() As List(Of ClassMissile)
        Get
            Return missileList
        End Get
   

    obj2 -get  list and change values
---------

   Dim missiles As List(Of ClassMissile)
        missiles = Sprite.GetMissiles
          For Each Mymissile In missiles
...
mymissile.y=0 'is this changing the list property as i think it should?
----------------------
0
Comment
Question by:jagguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24086108
You should be changing the actual list, but the easiest way to test is this:
' Get the list
Dim missiles1 As List(Of ClassMissile) = Sprite.GetMissiles()
 
' Change an item
missiles1(0).y = 123456
 
' Get the list again
Dim missiles2 As List(Of ClassMissile) = Sprite.GetMissiles()
 
' Compare y for the 1st item
Dim b As Boolean = (missiles1(0).y = missiles2(0).y)

Open in new window

0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24086134
You are changing acutal list.
0
 

Author Comment

by:jagguy
ID: 24086166

When i pass the list am i passing the whole data around ?

IF I am passing the whole data around i could as well use a public list in a module.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 15

Expert Comment

by:oobayly
ID: 24086263
Well, technically you're returning a reference to the object data.
As for modules, I'd stay clear of them, and stick with Object Orientated code.
0
 
LVL 6

Expert Comment

by:HarryNS
ID: 24086407
If you need to work with the list that contains value without changing the list, use <your LIst>.CopyTo(Array). This will copy the values to new array. There you can change values and use.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24088558
"When i pass the list am i passing the whole data around?"

As oobayly stated you are only passing a reference.  Stated differently, you are NOT passing the entire List around...just the pointer to the List (but we don't deal with pointers in managed .Net so they are called "references").  Think of it as passing the key to the front door of a house.  It allows you access to the house without passing the entire house....
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24088583
@Idle_Mind
I like that analogy.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question