PC Review


Reply
Thread Tools Rate Thread

C# - initializing an array within a structure?

 
 
Doug C via .NET 247
Guest
Posts: n/a
 
      22nd May 2004
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


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

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

<Id>WgwdRHYfKE+hgFBFJYm1hw==</Id>
 
Reply With Quote
 
 
 
 
Eric Marvets
Guest
Posts: n/a
 
      22nd May 2004
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 Removed) for Information on Our Architecture and
Mentoring Services

</shameless self promotion>


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Initializing many elements in a structure - memcpy? Pavel Minaev Microsoft C# .NET 0 2nd Jul 2008 10:10 PM
Re: Initializing many elements in a structure - memcpy? Jon Skeet [C# MVP] Microsoft C# .NET 0 2nd Jul 2008 01:17 PM
Initializing structure arrays. Steve Microsoft VB .NET 2 14th Sep 2005 12:29 AM
Re: Copying unmanaged memory pointer of structure to managed structure array Mattias Sjögren Microsoft VC .NET 0 28th Aug 2004 03:19 PM
Structure array inside structure when passing reference to C++ Mikko Penkkimäki Microsoft C# .NET 1 29th Oct 2003 03:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:39 AM.