简体   繁体   中英

VB.net replacing a byte array in a list of structures

I have this structure.

Public Structure struct1
    Public iNum as integer
    Public previous As Byte()
    Public seconds As Integer
    Public nextRead As DateTime
End Structure

I then have this list of struct1

Dim lStruc1 As List(Of struct1) = New List(Of struct1)

I initialize the structure by using this

    lStruct1.Add(New struct1 With {.iNum = 1, .previous = New Byte() {}, .seconds = 30, .NextRead = DateTime.Now})
    lStruct1.Add(New struct1 With {.INum = 2, .previous = New Byte() {}, .seconds = 30, .NextRead = DateTime.Now})
    lStruct1.Add(New struct1 With {.INum = {what ever data port}, .previous = New Byte() {}, .seconds = 30, .NextRead = DateTime.Now})

I read through the list doing this

        For Each str As struct1 In lStruct1
            If str.NextRead < DateTime.Now Then
               '  do read of data
               If str.previous.SequenceEqual(bNewData) = False Then
                   Array.Resize(str.previous, bNewData.Length)
                   Array.Copy(bNewData, str.previous, bNewData.Length)
                   '  write out new data(bNewData) to a file
               End If
            End If
         Next

The problem is that it doesn't update the item.previous in the list lStruc1. I can change a byte in str.previous and the list item in lStruct1 will update as well. Setting str.previous to New byte() disconnects the str.previous from the list of lStruct1. With the type of structure I am using, I am not able to set an initial size as I get compiler errors.

All I need is a list of Items I am monitoring that are an array of bytes and write that list of bytes to a log when anyone of the 100's points I am monitoring changes. I am also keeping seconds as an average time that any one of these 100's of points change at different times from 10 seconds to upto 4 hours, so I keep an average of seconds of change not to continually read them.

How do I update the byte array.previous in the lStruct1 list?

I would also love to do it like this

        For Each str As struct1 In lStruct1.findall(function(f) f.NextRead < datetime.now)

        Next

So I don't do a check on each item. Right now I go through the entire list creating a new list and then setting lStruct1 to the new list. I know what I am doing above with.findall is almost the exact same thing as the code above, but I think it looks cleaner to do the findall way. It is not possible to do it that way because at the moment I have to create a new list of all items to update lStruct1 when complete of reading the ports that need to be read.

I know that there is an easier way to do this.

I fixed this by defining lStruct1 as

Dim lStruc1() as struct1

lStruct1 = new struct1() {New struct1 With {.iNum = 1, .previous = New Byte() = New Byte() {}, .seconds = 30, .NextRead = DateTime.Now},
                      New struct1 With {.iNum = 2, .previous = New Byte() = New Byte() {}, .seconds = 30, .NextRead = DateTime.Now},
                      New struct1 With {.iNum = {what ever data port}, .previous = New Byte() = New Byte() {}, .seconds = 30, .NextRead = DateTime.Now} }

instead of

Dim lStruc1 As List(Of struct1) = New List(Of struct1)

I then changed the looping code to this

    For x as integer = 0 to lStruct1.Count - 1
        If lStruct1(x).NextRead < DateTime.Now Then
           '  do read of data
           If lStruct1(x).previous.SequenceEqual(bNewData) = False Then
               lStruct1(x).previous = bNewData
               
               dim sFilename as string = "{log port number - date and time}" + ".log"
               File.WriteAllBytes(sFilename, bNewData)
           End If
        End If
     Next
    
     Thread.Sleep(1000)

It now works like a champ.

As for naming conventions, it is my own code, nobody but me will ever see it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM