By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}
}

Jun 27 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This link should help you -

http://forums.microsoft.com/MSDN/Sho...59108&SiteID=1

Thanks -

<sh******@yahoo.com.auwrote in message
news:11**********************@g37g2000prf.googlegr oups.com...
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;
}
}

Jun 27 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.