Array in structure

  • Thread starter Thread starter Mateusz [PEYN] Adamus
  • Start date Start date
M

Mateusz [PEYN] Adamus

Hi.

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

<c#>
[StructLayout(LayoutKind.Sequential, Pack=2)]
internal struct TwEnumeration
{
public ushort ItemType;
public uint NumItems;
public uint CurrentIndex;
public uint DefaultIndex;
[MarshalAs(UnmanagedType.ByValArray, 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
 
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.
 
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(LayoutKind.Sequential, Pack=2)]
internal struct TwOneValue
{
public short ItemType;
public int Item;
}

TwOneValue res = new TwOneValue();
res = (TwOneValue)Marshal.PtrToStructure(Twain.GlobalLock(cap.Handle),
typeof(TwOneValue));
</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(LayoutKind.Sequential, 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
 
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)Marshal.PtrToStructure(Twain.GlobalLock((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
 

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

Back
Top