How to define a Class with fixed size array of sub structures

S

shofu_au

Hi Group,

I am trying to define a class that has a fixed size array of a
structure containing a fixed size array of a structure.

I am using System.Runtime.InteropServices and trying to define the
fixed size using [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]

In the attached console application example when I try to address the
fields in the class I get an execption.
Also when I try to examine the sub structures in the debugger I get
NULL.

So I assume I need to use new to create the fields - I have tried
several combinations without any success.

Does anyone konw what I am doing wrong in this example?

Thanks

Mark

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ArrayofStruct
{
class Program
{
static void Main(string[] args)
{
toplevel firstlevel = new toplevel();

// firstlevel.

firstlevel.level1[1].subelevel1_array[1].sublevel2_array[1] = 0;
}
}

public struct sublevel2 //Sublevel 2 struct containing array 10 of
UInt16
{
public UInt16 sublevel2_field1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public UInt16[] sublevel2_array;
}

public struct sublevel1 //Sublevel 1 struct containing array of
10 of level2 struct
{
public UInt16 sublevel1_field1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public sublevel2[] subelevel1_array;
}

public class toplevel //Toplevel Class containing array 10 of
level1 struct
{
public UInt32 field1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public sublevel1 [] level1;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top