المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to define an inline array of a contstant size in a C# structure



C# Programming
09-30-2009, 09:30 PM
Hi,

How to define an inline array size inside the stucture.Sample code i'm attaching the code block.
I'm using MarshalAs unmanaged it's working fine.
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Seque ntial)]
public struct tPacket {
/// WORD->unsigned short
public ushort word1;
/// WORD->unsigned short
public ushort word2;
/// BYTE->unsigned char
public byte byte1;
/// BYTE->unsigned char
public byte byte2;
/// BYTE[8] [
System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByVal Array, SizeConst=8, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
public byte[] array123;}
And i used a struct which has a create method that fills out the array but it thow an error
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Seque ntial)]
public struct tPacket {
public ushort word1;
public ushort word2;
public byte byte1;
public byte byte2;
public byte[] array123;
public static tPacket Create() {
return new tPacket() { array123 = new byte[8] }; }}

please help me.
With Regards
Mahesh