How to create the unnamed unions and structures in delphi?

sudhakar_koundinya
sudhakar_koundinya used Ask the Experts™
on
Hi all,


How to create the unnamed unions and structures in delphi

for e.g.

struct                  
{              
     int i;          
     double x;          
     char* str;          

};              

union                                  
{                                      
     int i;                              
     double x;                            
     char* str;                          
     struct someVatiable;                
};                                    
     
thanks
Koundinya                        



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004
Commented:
not possible, each structure must be named
The above is wrong even in C.

Author

Commented:
i agree with u, but this works in c++



struct MyStruct
{

    union
    {
     int booln;          
     int intg;              
     double real;          
     int blk;              
    };
};

class MyClass
{

 union {              
     int intg;              
     double real;              
        char *name;              
     char *cmd;              
     };
};
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Top Expert 2004

Commented:
well, don't know waht union means,
but here some possibilities

TRecord = Record
             a : Integer;
             b : String;
          end;

TOtherRecord = Record
                 a : Integer;
                 b : String;
                 c : TRecord;
               end;

TAnotherOneRecord = Record
                      a : Integer;
                      b : String;
                      c : TRecord;
                      d : Record
                            a : Integer;
                            b : String;
                          end;
                     end;  
                           
meikl ;-)
The easiest way in Delphi is to make the union its own full variant record. Then it needs of course its own type name.
It is bad style anyway to use unnamed structures.
union is a record with variant part.
Top Expert 2004

Commented:
>union is a record with variant part.
? something like

type
  TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
  TFigure = record
    case TShapeList of
      Rectangle: (Height, Width: Real);

      Triangle: (Side1, Side2, Angle: Real);
      Circle: (Radius: Real);
      Ellipse, Other: ();
  end;

just pasted from the delphi-online-help
to use variant records of course:

type
  TEmployee = record
  FirstName, LastName: string[40];
  BirthDate: TDate;
  case Salaried: Boolean of
    True: (AnnualSalary: Currency);
    False: (HourlyWage: Currency);
end;

AnnualSalary and HourlyWage will be share the same physical memory, so it will be identical to C union

Top Expert 2004

Commented:
someone else, who looks in the delphi online help, or, stepashka?

Author

Commented:
:-)
Hmmm... RTFM is my motto! Read the F@#$ing Manual (sorry)

Commented:
LOLROF
:(   I'll never read the help and cite it...

Author

Commented:
Acceping your answer, as i agree with ur first comment

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial