|
-
Nov 25th, 2009, 04:41 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Problem with changing data in a structure
Hi all,
I have some structures and I change the data inside a loop. This works ok. But as soon as the method ends, all values in the structure are reset???
Does anyone have an idea why this is?
Heres my code:
Code:
Private Sub ReadDevice(ByRef device As Device)
Console.WriteLine(" Reading device " & Device.name)
Dim data As Short
PLC.ReadDeviceBlock2(Device.name, 1, data)
Dim temp = Convert.ToString(data, 2).ToCharArray
Array.Reverse(temp)
Dim word = Convert.ToString(temp)
Console.WriteLine(" Word : " & word)
For Each Element As Element In Device.elements
Dim bitStatus As Boolean = Convert.ToBoolean(Convert.ToByte(word(Element.index).ToString))
If bitStatus.Equals(True) AndAlso Element.status.Equals(False) Then
Element.status = True
Element.timestamp = Now
ElseIf bitStatus.Equals(False) AndAlso Element.status.Equals(True) Then
Element.status = False
Element.totalSeconds = Convert.ToInt32((Now - Element.timestamp).TotalSeconds)
End If
Console.WriteLine("Index : " & String.Format("{0:00}", Element.index) & " Status : " & Element.status & " Timestamp : " & Element.timestamp.TimeOfDay.ToString & " TotalSeconds : " & Element.totalSeconds)
Next
End Sub
Private Structure Lane
Dim description As String
Dim devices As List(Of Device)
End Structure
Private Structure Device
Dim name As String
Dim elements As List(Of Element)
End Structure
Private Structure Element
Dim index As Byte
Dim status As Boolean
Dim timestamp As DateTime
Dim totalSeconds As Integer
End Structure
End Module
Don't pay too much attention at what is going on. It's just that when I assign a value: Element.TimeStamp = Now it will be 00/00/0000 12:00:00 again when I exit the sub :S.
[EDIT]
Method that calls ReadDevice() :
Code:
Private Sub ReadLane(ByRef lane As Lane)
Console.WriteLine(Environment.NewLine & "Reading lane " & Lane.description)
For Each device As Device In Lane.devices
ReadDevice(device)
Next
End Sub
Last edited by gonzalioz; Nov 25th, 2009 at 04:45 AM.
-
Nov 25th, 2009, 05:13 AM
#2
Re: Problem with changing data in a structure
Structures are value types. When you retrieve an instance of a structure from some collection, you are actually making a copy of it's value. You can then change that value all you like, but the original structure, which is still in the collection, is never touched.
Instead, use a Class. Classes are reference types, which means that if you retrieve an instance of a class from some collection, you are retrieving a copy to its memory location. If you change that instance, you are actually modifying the class instance on that copied memory location (which points to the very same object) so you are modifying the instance in the collection, like you want.
-
Nov 25th, 2009, 05:21 AM
#3
Re: Problem with changing data in a structure
 Originally Posted by NickThissen
Structures are value types. When you retrieve an instance of a structure from some collection, you are actually making a copy of it's value. You can then change that value all you like, but the original structure, which is still in the collection, is never touched.
Quite true.
 Originally Posted by NickThissen
Instead, use a Class. Classes are reference types, which means that if you retrieve an instance of a class from some collection, you are retrieving a copy to its memory location. If you change that instance, you are actually modifying the class instance on that copied memory location (which points to the very same object) so you are modifying the instance in the collection, like you want.
That's one solution but you can certainly still edit structure instances in a collection, e.g.
vb.net Code:
Dim instance As SomeStructure = myList(index)
instance.SomeProperty = someValue
myList(index) = instance
You simply overwrite the original with the copy once you're done editing.
-
Nov 25th, 2009, 05:34 AM
#4
Re: Problem with changing data in a structure
Yes, I suppose you could do that. I don't like it much as I tend to forget stuff like that and then spend lots of time figuring out why it isn't working
-
Nov 25th, 2009, 05:44 AM
#5
Re: Problem with changing data in a structure
It should also be noted that, using the method I suggested, the original code would have to be changed to use a For loop rather than a For Each loop, providing the ability to assign back to the collection.
-
Nov 25th, 2009, 07:29 AM
#6
Thread Starter
Hyperactive Member
Re: Problem with changing data in a structure
K, thnx! I got it to work now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|