Newsgroups: comp.lang.pascal Path: utzoo!utgpu!watserv1!watmath!nmouawad From: nmouawad@watmath.waterloo.edu (Naji Mouawad) Subject: Re: need a little flexibility Message-ID: <1991Apr26.152604.23483@watmath.waterloo.edu> Organization: University of Waterloo References: <1991Apr24.183315.7997@ux1.cso.uiuc.edu> <1991Apr24.235739.25115@watmath.waterloo.edu> <91115.111155IO92203@MAINE.BITNET> Date: Fri, 26 Apr 1991 15:26:04 GMT Lines: 76 In article <91115.111155IO92203@MAINE.BITNET> IO92203@MAINE.BITNET (Scott Maxell) writes: >>Block = record >> x : byte; >> y : byte; >> case boolean of >> True : (data : array[1..252] of byte); >> False: (dataw: array[1..126] of word); >>end; >> >>would do the trick. You could access just as easily data or dataw >>as follows: >> >>Var B : block; >> w : word; >> >>Begin >> w := 12345; >> b.data[i] := hi(w); >> b.dataw[i] := w; >>end. >> > > This last approach won't work as stated because a variant record can >only contain one of the possible fields declared. The field in the variant >should also have a declared variable and not just a type. > >Type > Block = RECORD > X, > Y : Byte; > Case WordData : Boolean OF > True : ( DataW : ARRAY [1..126] OF Word ); > False : ( Data : ARRAY [1..252] OF Byte ); > END; (* record *) > >VAR > WordData : BOOLEAN; > OneWord : Word; > OneByte : Byte; > >BEGIN > B.WordData := True; (* or false depending on what your data is. *) > OneByte := ????; (* Your byte data. *) > OneWord := ????; (* Your word data. *) > n := ????; (* Array index. *) > Case B.WordData OF > True : B.Data [n] := OneByte; > False : B.DataW [n] := OneWord; > END; (* case *) >END; > > When adding data to a variant record, there needs to be a value placed in >the tag field, or you will generally get an error. > Hi Scott, While this may be true in Pascal, it is not the case in Turbo Pascal: "An *optional* identifier, the tag field identifier, can be placed in the variant part. If a tag field identifier is present, it becomes the identifier of an additional fixed field - the tag field - of the record. The program can use the tag field's value to show which variant is active at a given time. Whithout a tag field, the program selects a variant by another criterion." TP V 6.0 programmer's manual, page 31. Cheers, --Naji. -- ------------------------------------------------------------------- | Naji Mouawad | nmouawad@watmath.waterloo.edu | | University |---------------------------------------------------| | Of Waterloo | "The Stranger in us is our most familiar Self" |