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#
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnAuthor Commented:
I found the answer

An array needs to be dimensioned to use it.  

I was trying to put in the byte array (the jagged part) without the first part being dimensioned.  

I redim'd the first part and then I could add the Flowsets

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnAuthor Commented:
Thanks to anyone who looked....
JohnAuthor Commented:
ReDim stcNetflowPacket.Flowset(1)

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.