473,670 Members | 2,425 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.Expl icit 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 .InteropService s.StructLayout
(System.Runtime .InteropService s.LayoutKind.Ex plicit)>
Public Structure IPCInitReqUnion
<System.Runtime .InteropService s.FieldOffset(0 )
Dim s As String <System.Runtime .InteropService s.FieldOffset(0 ) Dim HEADER As MyHeaderClass

<System.Runtime .InteropService s.FieldOffset
(88)> Dim USERID As String
<System.Runtime .InteropService s.FieldOffset
(120)> Dim PASSWORD As String
<System.Runtime .InteropService s.FieldOffset
(152)> Dim LocalLogon As Short
<System.Runtime .InteropService s.FieldOffset
(154)> Dim FTPProcessName( ) As String
<System.Runtime .InteropService s.FieldOffset
(184)> Dim NodeCount As Short
End Structure
Nov 20 '05 #1
1 6316
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.Expl icit 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.Runtim e.InteropServic es.StructLayout
(System.Runtime .InteropService s.LayoutKind.Ex plicit)>
Public Structure IPCInitReqUnion
<System.Runtime .InteropService s.FieldOffset(0 )
Dim s As String

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

<System.Runtime .InteropService s.FieldOffset
(88)> Dim USERID As String
<System.Runtime .InteropService s.FieldOffset
(120)> Dim PASSWORD As String
<System.Runtime .InteropService s.FieldOffset
(152)> Dim LocalLogon As Short
<System.Runtime .InteropService s.FieldOffset
(154)> Dim FTPProcessName( ) As String
<System.Runtime .InteropService s.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(L ayoutKind.Seque ntial)> _
Public Structure IPCIReq
Dim Header As MyHeaderClass
Dim UserId As String
....
End Structure

<SturctLayout(L ayoutKind.Expli cit)> _
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
5274
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 thing? I guess there might be some additional overhead with CLASSES, but is that really an issue with today's computers?
8
1620
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 ways to do this ;-) ). What I need to know is how to write the function prototype and the function definition for a function that returns a union. For example: Here is a class for a double linked list in which variable data is going to be...
6
13377
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
3943
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 value is declared to a variable in the union, the existing value is overwritten even though the new value is declared to a different variable than that of the first value. Now I'm just wondering what the use of this is. I'm sure there are lots,...
23
2817
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 relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
4
1757
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
3339
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 sake of Unions simply because I wanted to see one in action. Granted: it makes it possible to save a few bytes of storage when you have something that can be a chicken or a rooster, but not both, and you're always going to know which it is. ...
26
1915
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 Unions" and "union {unsigned char u; ...} ". Here's a concrete example: #include <stdio.h> int main(void)
11
2009
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 unions . My questions are : 1. Is it dangerous to use unions ? Is it worth the trouble if I want to save memory ? Are they error prone ? 2. I read that it is not possible to access more than 1 member at any instant from a union. What does this...
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7412
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6212
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
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 we have to send another system

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.