473,394 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

problem with constructor of struct using FieldOffset

I have a struct that uses LayoutKind.Explicit to redefine bytes within
the struct:

[StructLayout(LayoutKind.Explicit)]
public struct PointerStoredValue
{
[FieldOffset(0)]
public int LayoutId;
[FieldOffset(4)]
public int HeapId ;
[FieldOffset(8)]
public int BufferBx;
[FieldOffset(12)]
public int EmptySpace;
[FieldOffset(0)]
public long Part1;
[FieldOffset(8)]
public long Part2;

public PointerStoredValue(byte[] InArray, int InBx)
{
Part1 = BitConverter.ToInt64(InArray, InBx);
Part2 = BitConverter.ToInt64(InArray, InBx + 8);
}
}

the constructor will not compile:
"field LayoutId must be fully assigned before control leaves the
constructor"

the Part1 field is a long from bytes 0 to 7 of the struct. field
LayoutId is an int from bytes 0 to 3. By assigning to field Part1,
field LayoutId is being assigned.

Why the compiler error?

thanks,

-Steve

Feb 16 '07 #1
2 2975
Steve Richter <St************@gmail.comwrote:

<snip>
the constructor will not compile:
"field LayoutId must be fully assigned before control leaves the
constructor"

the Part1 field is a long from bytes 0 to 7 of the struct. field
LayoutId is an int from bytes 0 to 3. By assigning to field Part1,
field LayoutId is being assigned.

Why the compiler error?
The compiler doesn't know about the FieldOffsetAttribute itself. If you
just assign the "duplicate" fields any value (before Part1 and Part2)
you'll get the desired effect.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 16 '07 #2
On Feb 16, 9:30 am, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Steve Richter <StephenRich...@gmail.comwrote:

<snip>
the constructor will not compile:
"field LayoutId must be fully assigned before control leaves the
constructor"
the Part1 field is a long from bytes 0 to 7 of the struct. field
LayoutId is an int from bytes 0 to 3. By assigning to field Part1,
field LayoutId is being assigned.
Why the compiler error?

The compiler doesn't know about the FieldOffsetAttribute itself. If you
just assign the "duplicate" fields any value (before Part1 and Part2)
you'll get the desired effect.
thanks for the confirmation.

Feb 16 '07 #3

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

Similar topics

10
by: Ady | last post by:
Hi, I posted this a few days ago but it seems no one has replied any help much appreciated. I know that i have to serialise my class, How could I pull out the value off offsetTextures and...
8
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the...
15
by: Ken Allen | last post by:
I have some code from C/C++ that I am attempting to port to C#. I have come across an interesting problem that is quite common in complex C/C++ code: the us of UNION in structure definitions to...
6
by: erdem | last post by:
hi all, i guess this question had been asked many times but i couldnt find a way how can we generate a struct with managed code with fixed size i mean struct easy {
2
by: Lorenzo | last post by:
Hi I have a problem converting the follow union in c#: typedef union{ int iParam; char acParamChar; } Param; I tried with this but don't work... why?
3
by: Angel J. Hernández M | last post by:
Hi folks! I'm having a little problem and I'm a bit confused about it. See... I'm working on this VoIP project (I need to use make direct calls to Windows API). I started working using C# but since...
4
by: Rain | last post by:
Im using a UDPClient Send to send message to the server heres my code: MsgProperties _msgPro = new MsgProperties(); byte bs = System.Text.Encoding.UTF8.GetBytes( _msgPro._seqNo + _msgPro._msgLen...
0
by: O.B. | last post by:
I have the following struct that I'm trying to get to behave like a union, where Unsigned* and Float* are structs. unsafe public struct Bits64 { public fixed Unsigned8 c; public fixed...
2
by: dave2118 | last post by:
Hello! I am working on conversion from C++ to C#. One of the pieces of code involves a Union Structure which doesn't seem to exist in C#. Old C code typedef struct { char char1; char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.