473,672 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>
Jul 21 '05 #1
1 8153
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 sa***@bangproje ct.com for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4776
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data structures are read only) Ideally one should be able to put the redundant information there automatically so no mistakes are possible, but in a lot of case I see no way how to do it.
2
2997
by: Steve | last post by:
I have defined a structure of; Private Structure Menu Private MenuID as string Private Label as string End Structure ....and I would like to create and initialize an array of type "Menu". I have tried numerous variations on the theme;
1
436
by: Doug C via .NET 247 | last post by:
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 {
104
16934
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
7
2097
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
12
2282
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
4
10535
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1 = 100; unionMember.union1.field2 = 200 }; fullStructType var2 = { { unionMember.union2 = 300 } , { unionMember.union2 = 400 } };
5
3717
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non -cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a normal tree. Now the thing is i can popullate my tree at compile time like a global data. Since i know my tree definition at compile time, instead of using pointers to point to siblings or child nodes, i am planning to use the array with...
2
2254
by: Martin Payne | last post by:
I am trying to initialise a structure with random values for all its fields. It is a large structure so I do not want to do it for each element in turn (it would take ages). Please note my structure has the qualification: so it is effectively a block of memory of size 512 bytes although it has integer and string fields. What I would like to do is something like this:
0
8486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.