PROBLEM:
I’m trying access binary records that were created in Borland Delphi and stored in a SQL Server database (as a BLOB).
Q: What the heck is the syntax for accessing a two-D array in C#????
Here’s an example:
const MAX_BOWLERS = 8; gMAX_FRAMES = 40; ...type
TFrame = Record Balls : array[1..3] of ShortInt; // Pins standing: balls 1, 2 and 3 Pins : array[1..3] of ShortInt; CurrentBall : Byte; Score : Integer; // Current score (-1= undefined) Attributes : TFrameAttributes; ...
TFrames = Array[1..Max_Bowlers, 0..gMax_Frames] of TFrame;
TgameRec = Record Side : Byte; Bowlers : tBowlers; Frames : TFrames; ...
Soooooooo….
I’ve successfully got a valid “GameRec” over to C#-land.
I want to access GameRec.Frames[iBowler, iFrame].
Q: How do I define a C# type “TFrames = Array[1..Max_Bowlers, 0..gMax_Frames] of TFrame;” so that I can do it?
Thank you very much in advance .. PSM
I found a solution:
Treat the 2-D array as its own struct, containing an array.
The contained array is 1D, consisting of cols * rows elements
Provide a C# “indexed property” so that external clients can access elements as though they were in a 2-D array (which, in terms of memory layout, they actually are!)
// C# Definition for Delphi 2-D array [StructLayout(LayoutKind.Sequential, Pack = 4)] public unsafe struct TFrames { [MarshalAs(UnmanagedType.ByValArray, SizeConst=(MAX_BOWLERS)*(gMAX_FRAMES+1))] private TFrame[] row; public TFrame this[int iBowler, int iFrame] { get { int ioffset = (iBowler * (gMAX_FRAMES+1)) + iFrame; return row[ioffset]; } } }// C# client example public static string ConvertSplitToString(TgameRec currentGame, int iBowler) { StringBuilder sb = new StringBuilder(); TFrames frames = currentGame.frames; for (int iFrame = 0; iFrame < 10; iFrame++) { if (frames[iBowler, iFrame].fSplit != 0) sb.Append('.'); else sb.Append(' '); } return sb.ToString (); }