• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Converson of C- UNION struct into VB

How does one convert the following C structure into VB type. Especially the UNION. I have to keep the same type .Thanks

typedef struct ELEMENT2
{
  int type;                            
  int flip;                          
  char pc1_stkno[65];
  double wt;
  union
  {
    struct    
    {
      double od;              
      double id;              
      double thk;              
      double hod;              
      double hthk;            
      double fod;              
      double fthk;            
      double bc;              
      double bh;
      double collar_od;        
      double collar_id;        
      double collar_length;
    } fr;                        
    struct        
    {
      double od;              
      double id;              
      double thk;              
      double hod;              
      double hthk;            
      double bc;              
      double bh;
      double collar_od;        
      double collar_id;        
      double collar_length;
    } ff;                    
   } params;
} entity;
0
rian
Asked:
rian
  • 3
1 Solution
 
AzraSoundCommented:
try just declaring as a Variant, e.g.,


Type ELEMENT2
   type As Integer
   flip As Integer
   pc1_stkno As String
   wt As Double
   params As Variant
End Type
0
 
AzraSoundCommented:
you may have problems coercing your two UDTs that make up the union into a Variant data type.  if so, you will probably want to work with classes instead.  create your interface class sharing the common properties between the two unioned structures, and then create two classes to represent those structures which implement your common interface. e.g.,


'-- IParams class

Property Get od() As Double
End Property
Property let od(ByVal Newod As Double)
End Property

Property Get id() As Double
End Property
Property Let id(ByVal Newid As Double)
End Property

'etc for the COMMON values the two structures share



'-- Structure1 class

Implements IParams

'fill in required values for interface

'then add properties not included in the interface



'-- Structure2 class

Implements Iparams

'fill in required values for interface - this should cover all of the properties the class requires




then your common type can be:

Type ELEMENT2
   type As Integer
   flip As Integer
   pc1_stkno As String
   wt As Double
   params As IParams
End Type


and youll be able to pass as the params argument, either an instance of Structure1 or an instance of Structure2
0
 
WolfgangKoenigCommented:
You can't restict string datatype in VB and there is no
union datatype only type...
Your struct is in VB the following:
Type frfr
    od As Double
    id As Double
    thk As Double
    hod As Double
    hthk As Double
    fod As Double
    fthk As Double
    bc As Double
    bh As Double
    collar_od As Double
    collar_id As Double
    collar_length As Double
End Type

Type ffff
    od As Double
    id As Double
    thk As Double
    hod As Double
    hthk As Double
    bc As Double
    bh As Double
    collar_od As Double
    collar_id As Double
    collar_length As Double
End Type

Type paramsparams
  fr As frfr
  ff As ffff
End Type

Type ELEMENT2
 type As Integer
 flip As Integer
 pc1_stkno As String
 wt As Double
 params As paramsparams
End Type

Dim Entity As ELEMENT2

Nice day!
WoK
0
 
AzraSoundCommented:
well if the idea is to be able to pass an object arbitrary of its type, then you will need to use interfaces.  otherwise, as mentioned, you will just have to implicitly declare each type you want to use in your structure.
0
 
Richie_SimonettiIT OperationsCommented:
hearing...
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now