troubleshooting Question

Jagged array in structure, C# or VB

Avatar of John
JohnFlag for United Kingdom of Great Britain and Northern Ireland asked on
Visual Basic.NET.NET ProgrammingC#
3 Comments1 Solution176 ViewsLast Modified:
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#
ASKER CERTIFIED SOLUTION
John

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros