Doug Semler
9/3/2007 8:13:00 PM
<MLM450@hotmail.com> wrote in message
news:1188224542.423225.97450@19g2000hsx.googlegroups.com...
> Is it possible to retrieve an item's "sizeconst" value dynamically?
>
> I have the following member defined in a class:
> [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
> private char[] units;
>
> I want the code that values the units member to know the value of
> SizeConst. I know I can define the size in another member but
> accessing the SizeConst would make things much cleaner for what I am
> trying to do.
Why? SizeConst (as hinted by the name) is a constant. You are telling the
marshaller that you will be marshalling a 5 element array. Everything
(including your initializer of units = new units[5]) must use that constant
size. If possible, I would also make the units member readonly (I can't
remember if this is possible with interop though).
For .NET code, you can use a
const int unitArraySize = 5;
and reference that value anywhere you need to know the size of the array:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = unitArraySize)]
private char[] units = new units[unitArraySize];
void function
{
// I need to know the size. you can use unitArraySize, but I
personally would use units.Length.
}
--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?