Home All Groups Group Topic Archive Search About

Using Find method on a List(of type) Breaks the Reference

Author
29 Mar 2006 3:40 PM
muskito.net
Hi,

I Have the following code:

    Public Sub UpdateEvent(ByVal ID As Integer, _
                           ByVal StartTime As Integer, _
                           ByVal Duration As Integer)
        EventId = ID
        Dim EventToUpdate As SchedulerEvent =
SchedulerEvents.Find(AddressOf FindById)
        EventToUpdate.StartTime = StartTime
        EventToUpdate.Duration = Duration
    End Sub

SchedulerEvent is a Structure
SchedulerEvents is a List(of SchedulerEvent)

When i run this, the Find method Does find the correct object
corresponding to the ID Supplied.
However - When I Update the StartTime and Duration of the object - it
does not update the actual list...
when i tried to check whether the reference between the two equals
using:
SchedulerEvent.ReferenceEquals(SchedulerEvents(1),EventToUpdate)
(I Only added two items to the list and tried to update the second one)
It was found to be False (this explains why the values aren't actually
beeing updated in the list).

My Questions are:
1. Why doesn't it keep the reference, and making updates like such will
work?
2. Is there something wrong with my code?
3. Is there a WorkAround you know that might work?

Thanks alot...

Author
29 Mar 2006 4:11 PM
Mattias Sjögren
>SchedulerEvent is a Structure
                     ^^^^^^^^^^

There's your problem. Since it's a structure (a value type), there are
no references involved. Copies of the value are passed around and
changing one doesn't affect other copies.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
29 Mar 2006 4:24 PM
Muskito
Ok, I'm A Bit shocked... :)

Is there another Structure like type that can use references?

Thanks for the quick answer!
Author
29 Mar 2006 4:34 PM
Muskito
Forget my question... the answer is simple...
All i need to create is a class.... a seperate class for that type...

Thanks anyway!