C# - initializing an array within a structure?

  • Thread starter Doug C via .NET 247
  • Start date
D

Doug C via .NET 247

Using C#...
I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is an array of another predefined structure, and the otheris simply an array of ushort's.
Such as:

public struct predefinedStruct {
public ushort a;
public ushort b;
}

public struct struct1 {
public int blah1;
public int blah2;
public predefinedStruct [] ARRAY1; // <-- size = 6
}

public struct struct2 {
public ushort blah1;
public ushort blah2;
public ushort [] ARRAY2; // <-- size = 32
}

...

for my pointer to this structure in memory to match thispredefined structure in my code, I need the size of my structurein my code to match the size of the structure in memory. Sotherefore I need to initialize my arrays. But I can't seem toinit them in the struct.

How can I initialize my two arrays, ARRAY1, and ARRAY2, in thiscase? The size of each array is:
ARRAY1 = 6 elements
ARRAY2 = 32 elements
 
E

Eric Marvets

You can do it one of 2 ways, make it a class or provide a parameterized
constructor. I know the length is predefined, but can you just accept the
length anyway?

public struct struct1 {
public int blah1;
public int blah2;
public predefinedStruct [] ARRAY1; // <-- size = 6

public struct1(int iSize)
{
ARRAY1 = predefinedStruct[iSize];
}
}

--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email (e-mail address removed) for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
 

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