Avatar of John
John
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Jagged array in structure, C# or VB

I am writing some network stuff to decode UDP packets.

I am having trouble working with a jagged array in a structure.  I need to put a header in the structure as a byte array and an array of Flowsets each as a byte array.  

I have a stucture like this:

    Public Structure udtNetflowPacket
        Dim Header() As Byte
        Dim Flowset()() As Byte
        Dim FLowsetType() As Integer ' 1=template, 2=data
    End Structure

Open in new window


I am having difficulty filling Flowset

byPacket is a byte array filled elsewhere

I have already taken the first 20 bytes and filled Header, So I am at byte position 19 (intCurrentByte)

        'Declare an instance of my structure
        Dim stcNetflowPacket As udtNetflowPacket

        'Define the header
        Dim byHeader(20) As Byte 'Header is 20 bytes long

        'Copy the header
        byHeader = New Byte(19) {}
        Buffer.BlockCopy(byPacket, 0, byHeader, 0, 20)
        stcNetflowPacket.Header = byHeader
        intCurrentByte += 20

Open in new window


 All good so far...  It then follows on to get a flowset (this bit will be in a loop as there could be many, but for now, it's just getting the first one)...

        
        Dim intFlowsetLength As Integer = 0
        Dim byFlowsetLength(2) As Byte

        ' This gets the length in bytes that I want to copy as an integer
        byFlowsetLength = {Buffer.GetByte(byPacket, intCurrentByte + 3), Buffer.GetByte(byPacket, intCurrentByte + 2)} 
        intFlowsetLength = BitConverter.ToInt16(byFlowsetLength, 0)

        'Copy the flowset
        Dim byFlowset(intFlowsetLength) As Byte
        byFlowset = New Byte(intFlowsetLength - 1) {}
        Buffer.BlockCopy(byPacket, intCurrentByte, byFlowset, 0, intFlowsetLength - 1)
        stcNetflowPacket.Flowset(0) = byFlowset

Open in new window


I get "Object reference not set to an instance of an object."

The last line causes the error  

I think this is because the array stcNetflowPacket.Flowset(0) has not been initialised.  I am having trouble with this bit.  

I don't know how many flowsets I am to get or how long each one will be, so I can't initialise it all upfront.

Any help please would be great

I've done it in VB as I find it easier to read, but I'm happy for assistance to be in C#
Visual Basic.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
John

8/22/2022 - Mon