[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

pass objects

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
jagguy
Asked:
jagguy
1 Solution
 
oobaylyCommented:
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
 
Sreedhar VengalaCommented:
You are changing acutal list.
0
 
jagguyAuthor Commented:

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
oobaylyCommented:
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
 
HarryNSCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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
 
oobaylyCommented:
@Idle_Mind
I like that analogy.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now