473,388 Members | 1,220 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,388 software developers and data experts.

problem with Union in C#

Hi
I have a problem converting the follow union in c#:

typedef union{
int iParam;
char acParamChar[128];
} Param;
I tried with this but don't work... why?

[StructLayout(LayoutKind.Explicit)]
struct Param
{
[FieldOffset(0)]
public int iParam;
[FieldOffset(0)]
public char []acParamChar;
}

There's someone that can help me?
Thank you.

Nov 17 '05 #1
2 1587
Lorenzo,

Two things here. First, when you declare an array, it is declaring the
structure with pointer to the array. You want it embedded in the array.

Second, since this is an array of characters, it's really a string, so
you can declare it as such.

So, in the end, you want this:

[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Ansi)]
struct Param
{
[FieldOffset(0)]
public int iParam;
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string acParamChar;
}

Notice the CharSet property on the StructLayout attribute. This
indicates that you want to use Ansi characters when converting character
types (which you want, since your unmanaged function is using the char type,
which is an 8-bit character).

Also, notice the SizeConst parameter on the MarshalAs attribute. This
will indicate the length of the array of characters which is embedded in the
structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Lorenzo" <lg***********@inwind.it> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi
I have a problem converting the follow union in c#:

typedef union{
int iParam;
char acParamChar[128];
} Param;
I tried with this but don't work... why?

[StructLayout(LayoutKind.Explicit)]
struct Param
{
[FieldOffset(0)]
public int iParam;
[FieldOffset(0)]
public char []acParamChar;
}

There's someone that can help me?
Thank you.

Nov 17 '05 #2
Thank you for your time,
but when I try to instantiate the class that
contain the Param structure there's an System.TypeLoadException
i don't know why ?

I hope that you know it.

Nov 17 '05 #3

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

Similar topics

6
by: Stephen Miller | last post by:
Firstly, sorry for the long post, but I've included a fair bit of sample data. Im doing a comparision of 10yr Bond prices and CPI adjustments, with an 18 week moving average of the CPI. I'm...
6
by: Bill | last post by:
I've created this: SELECT c.ProjectID, Count(c.ID) as 'Registrants', Count(dt.Hits) as 'Submissions' FROM CME_TBL c JOIN (SELECT ProjectID, Count(*) as Hits FROM CME_TBL
5
by: Dave | last post by:
I am working with a proprietary database that records the date, time, location, and speed of a vehicle. It is pulling this information from GPS unit tied to a vehicle. The table is populated with...
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
1
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
2
by: Gerry | last post by:
I am relatively new to DB2 and having a problem with a simple union statement. Running Db2UDB version 8.1.1 on Aix 5.1 The union and union all SQL statements I am running produce the same...
5
by: hubmei75 | last post by:
Hello, I have a simple table containing adresses. A sample view of the table is id name city -------------------------------- 100 Meier New York 101 Meier Tokyo 110 ...
5
by: 肥權 | last post by:
Hi everybody, I have a table storing the test dates, student names and the grade of the students. I am now going to find out those dates with student1 got A and student2 got B and vice versa...
4
by: google | last post by:
Hi Hope someone can help me with this - have been staring at this problem all day, and with the cold I've got, just don't seem to be able to figure it out! I've got two tables, here with some...
3
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.