Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Jagged array in structure, C# or VB

Avatar of John
JohnFlag for United Kingdom of Great Britain and Northern Ireland asked on
.NET ProgrammingC#Visual Basic.NET
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

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

 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

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#
Avatar of John
JohnFlag of United Kingdom of Great Britain and Northern Ireland image

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

This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers