473,513 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unions in VB.Net?

Does anybody know how to get unions to work in VB.Net?
What I'm trying to do is have a structure of a variety of
types of fields, and then I need to pass the whole thing
as a string later on. So I tried (see below) to use
LayoutKind.Explicit with FieldOffset, where I first set
the offset 0 to a string and then laid down the rest of
my fields. But instead of working like a union, it lays
the fields down sequentially (they don't overlap each
other as expected). Anybody know how to do this?

Thanks,

Joel
<System.Runtime.InteropServices.StructLayout
(System.Runtime.InteropServices.LayoutKind.Explici t)>
Public Structure IPCInitReqUnion
<System.Runtime.InteropServices.FieldOffset(0)
Dim s As String <System.Runtime.InteropServices.FieldOffset(0) Dim HEADER As MyHeaderClass

<System.Runtime.InteropServices.FieldOffset
(88)> Dim USERID As String
<System.Runtime.InteropServices.FieldOffset
(120)> Dim PASSWORD As String
<System.Runtime.InteropServices.FieldOffset
(152)> Dim LocalLogon As Short
<System.Runtime.InteropServices.FieldOffset
(154)> Dim FTPProcessName() As String
<System.Runtime.InteropServices.FieldOffset
(184)> Dim NodeCount As Short
End Structure
Nov 20 '05 #1
1 6305
In article <0c****************************@phx.gbl>, Joel wrote:
Does anybody know how to get unions to work in VB.Net?
What I'm trying to do is have a structure of a variety of
types of fields, and then I need to pass the whole thing
as a string later on. So I tried (see below) to use
LayoutKind.Explicit with FieldOffset, where I first set
the offset 0 to a string and then laid down the rest of
my fields. But instead of working like a union, it lays
the fields down sequentially (they don't overlap each
other as expected). Anybody know how to do this?

Thanks,

Joel
<System.Runtime.InteropServices.StructLayout
(System.Runtime.InteropServices.LayoutKind.Explici t)>
Public Structure IPCInitReqUnion
<System.Runtime.InteropServices.FieldOffset(0)
Dim s As String

<System.Runtime.InteropServices.FieldOffset(0)
Dim HEADER As MyHeaderClass

<System.Runtime.InteropServices.FieldOffset
(88)> Dim USERID As String
<System.Runtime.InteropServices.FieldOffset
(120)> Dim PASSWORD As String
<System.Runtime.InteropServices.FieldOffset
(152)> Dim LocalLogon As Short
<System.Runtime.InteropServices.FieldOffset
(154)> Dim FTPProcessName() As String
<System.Runtime.InteropServices.FieldOffset
(184)> Dim NodeCount As Short
End Structure


Joel,

If I understand what your trying to do, then you'll probably want to do
something like:

<StructLayout(LayoutKind.Sequential)> _
Public Structure IPCIReq
Dim Header As MyHeaderClass
Dim UserId As String
....
End Structure

<SturctLayout(LayoutKind.Explicit)> _
Public Structure IPCIReqUnion
<FieldOffset(0)> _
Dim s As String
<FieldOffset(0)> _
Dim ipr As IPCIReq
End Structure
If that isn't what your after, it would probably be helpful to see the
C/C++ declarations.
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2

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

Similar topics

15
5255
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
8
1603
by: SteveM | last post by:
The general consensus I am getting is that nobody really uses unions much (engineers here at work) but this is an academic exercise for me so I am looking for an answer (I know there may be better...
6
13363
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
3924
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
2791
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
4
1749
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
3300
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
1890
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
11
1995
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
7539
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...
1
7101
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
7525
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...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.