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

union in c#

Ali
Hi,

I'm a new programmer in C# but I know C/C++ programing,

I need somthing like 'union' in C#, what can I do?

thanks Ali
Nov 15 '05 #1
2 2052
Hi Ali,

You can probably try to simulate the equivalent of an union
by setting the [FieldOffset] attribute for all members
in a struct to the 0th position for the offset.

[StructLayout(LayoutKind.Explicit)]
public struct MyUnionWithCategories
{
[FieldOffset(0)] public int Category;
[FieldOffset(0)] public char CategoryRep;
[FieldOffset(0)] public long CategoryIndicator;
}

Regards,
Aravind C
"Ali" <mo*********@yahoo.com> wrote in message
news:6D**********************************@microsof t.com...
Hi,

I'm a new programmer in C# but I know C/C++ programing,

I need somthing like 'union' in C#, what can I do?

thanks Ali

Nov 15 '05 #2
Hi,
With the only one exception: You cannot have fields of a refence type in the
union

--
B\rgds
100

"Aravind C" <ar***********@nospam.hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP12.phx.gbl...
Hi Ali,

You can probably try to simulate the equivalent of an union
by setting the [FieldOffset] attribute for all members
in a struct to the 0th position for the offset.

[StructLayout(LayoutKind.Explicit)]
public struct MyUnionWithCategories
{
[FieldOffset(0)] public int Category;
[FieldOffset(0)] public char CategoryRep;
[FieldOffset(0)] public long CategoryIndicator;
}

Regards,
Aravind C
"Ali" <mo*********@yahoo.com> wrote in message
news:6D**********************************@microsof t.com...
Hi,

I'm a new programmer in C# but I know C/C++ programing,

I need somthing like 'union' in C#, what can I do?

thanks Ali


Nov 15 '05 #3

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

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.