473,666 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array in structure

Hi.

I have a problem. I'm creating a structure - looks like this:

<c#>
[StructLayout(La youtKind.Sequen tial, Pack=2)]
internal struct TwEnumeration
{
public ushort ItemType;
public uint NumItems;
public uint CurrentIndex;
public uint DefaultIndex;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=10)]
public short[] ItemList;
}
</c#>

It works OK but I have a problem with it:

1. How can I make its size flexible? Now if I'll get 11 results they
wan't fit into an array. And if I'll get 9 results one field in array
will be blank. How can I declare a dynamic array?

2. Can I put there an Array type? If so how? :-)

TIA

best regards
Mateusz [PEYN] Adamus
Nov 16 '05 #1
3 3278
A struct is a value type in C#. Value types have to have a fixed size, so
you're out of luck if you want to keep a variable-length array in the
struct. Your options as I see them:

1. Change from a struct to a class.
2. Keep TwEnumeration as a struct, but don't store ItemList as an array;
store it as a reference to a managed array. You'll have to use explicit
reference syntax (with & signs and stuff), and you'll have to go to extra
work to make sure that ItemList is set to a valid array reference, etc.
3. Make ItemList bigger than you think you'll need, like 100 elements or
something. Yes this wastes memory, but hey, RAM is cheap, right? Of course
this is not really a solution, but might work if you know that there's some
upper bounds on the number of results you'll ever need to store.
"Mateusz [PEYN] Adamus" <pe************ @tlen.pl> wrote in message
news:d1******** **@nemesis.news .tpi.pl...
Hi.

I have a problem. I'm creating a structure - looks like this:

<c#>
[StructLayout(La youtKind.Sequen tial, Pack=2)]
internal struct TwEnumeration
{
public ushort ItemType;
public uint NumItems;
public uint CurrentIndex;
public uint DefaultIndex;
[MarshalAs(Unman agedType.ByValA rray, SizeConst=10)]
public short[] ItemList;
}
</c#>

It works OK but I have a problem with it:

1. How can I make its size flexible? Now if I'll get 11 results they wan't
fit into an array. And if I'll get 9 results one field in array will be
blank. How can I declare a dynamic array?

2. Can I put there an Array type? If so how? :-)

TIA

best regards
Mateusz [PEYN] Adamus

Nov 16 '05 #2
Marcos Stefanakopolus napisa³(a):
A struct is a value type in C#. Value types have to have a fixed size, so
you're out of luck if you want to keep a variable-length array in the
struct. Your options as I see them:

1. Change from a struct to a class.

OK. Actually I have a Q about it :D

I have a structure in which I have a pointer to another structure. The
first structure I'm retreiving like this:

<C#>
[StructLayout(La youtKind.Sequen tial, Pack=2)]
internal struct TwOneValue
{
public short ItemType;
public int Item;
}

TwOneValue res = new TwOneValue();
res = (TwOneValue)Mar shal.PtrToStruc ture(Twain.Glob alLock(cap.Hand le),
typeof(TwOneVal ue));
</C#>

Sometimes Item is a handle to a structure which contains actuall data.
But sometimes it contains data. This structure is like this:

<C#>
[StructLayout(La youtKind.Sequen tial, Pack=2)]
internal struct TwFix32
{ // TW_FIX32
public short Whole;
public ushort Frac;
}
</C#>

And now the Q: How can I retrieve the TwFix32 structure from the Item
pointer?

Example from the top of mail does not work :-(

2. Keep TwEnumeration as a struct, but don't store ItemList as an array;
store it as a reference to a managed array. You'll have to use explicit
reference syntax (with & signs and stuff), and you'll have to go to extra
work to make sure that ItemList is set to a valid array reference, etc.
I don't want to mess with this unsafe code. I'm not very good in safe so
in unsafe I'll probably destroy something :D
3. Make ItemList bigger than you think you'll need, like 100 elements or
something. Yes this wastes memory, but hey, RAM is cheap, right? Of course
this is not really a solution, but might work if you know that there's some
upper bounds on the number of results you'll ever need to store.


Yep. I also came to this conclusion. If everything else fails I'll do it
like this but I want to try if meybe it can be done in some nicer way.
best regards
Mateusz [PEYN] Adamus
Nov 16 '05 #3
Mateusz [PEYN] Adamus napisa³(a):

OK. Actually I have a Q about it :D

I have a structure in which I have a pointer to another structure. The
first structure I'm retreiving like this:

[cut]

And now the Q: How can I retrieve the TwFix32 structure from the Item
pointer?

Example from the top of mail does not work :-(

Meybe this will help.

When I try to do it like this:

<C#>
TwFix32 fix32 = new TwFix32();
fix32 =
(TwFix32)Marsha l.PtrToStructur e(Twain.GlobalL ock((IntPtr)res .Item),
typeof(TwFix32) );
</C#>

i get this error:

---------------------------
Object reference not set to an instance of an object.
---------------------------

best regards
Mateusz [PEYN] Adamus
Nov 16 '05 #4

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

Similar topics

6
4193
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
5
21625
by: Joe Thompson | last post by:
Hi I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a structure I have populated. My question is, how do I do this in C# - that is, I want to declare a byte array, assign a structure to it, and then be able to populate the structure's fields, then use the byte array Thank you Joe
26
7068
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
104
16929
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
3161
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change thorughout the course the programs use so it will need to
8
2395
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct Employee { char employeeid; /* id of employee*/
2
2780
by: craig | last post by:
I'm fried on this problem...It's vb.net, dealing with a web service that requires input as an array of a particular data structure and I can't get the code to compile or run... Basically, the webservice is showing me the following: Public structure A public a1 as string end structure Public structure B
12
3872
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
5
3788
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
10
6364
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
0
8780
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7378
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6189
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
4192
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
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
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
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
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.