C# - initializing an array within a structure?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug C via .NET 247

    #1

    C# - initializing an array within a structure?

    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 predefinedStruc t {
    public ushort a;
    public ushort b;
    }

    public struct struct1 {
    public int blah1;
    public int blah2;
    public predefinedStruc t [] 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


    --------------------------------
    From: Doug C
    --------------------------------
    From: Doug C

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>WgwdRHYfKE+ hgFBFJYm1hw==</Id>
  • Eric Marvets

    #2
    Re: C# - initializing an array within a structure?

    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 predefinedStruc t [] ARRAY1; // <-- size = 6

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

    --
    Eric Marvets
    Principal Consultant

    the bang project

    <shameless self promotion>

    Email sales@bangproje ct.com for Information on Our Architecture and
    Mentoring Services

    </shameless self promotion>


    Comment

    Working...