AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

boolean ohne variable?

Ein Thema von Pseudemys Nelsoni · begonnen am 14. Dez 2004 · letzter Beitrag vom 20. Nov 2007
Antwort Antwort
Seite 1 von 4  1 23     Letzte »    
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#1

boolean ohne variable?

  Alt 14. Dez 2004, 21:39
hi,

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  fs : TFileStream;
  DataSize : Word;
  DataType : Word;
  CurEntry : record
    Header : record
      Tag : array[0..3] Of Char;
      EntryType : Word;
      Size : Word;
    end;
    case boolean of
      true: (Value : DWORD; Junk: array[0..1023] of Char);
      false : (Str : array[0..1023] of Char);
  end;
  CurInfo : String;
  CurJunk : array[0..1023] Of Char;
begin
  //...
end;
ich habe 2 fragen hierzu, ersteinmal: wieso hat das "case" hier kein "end;" ? und zweitens wie funktioniert der boolean in dem case, er hat ja gar keine variable? O_o
Mario
MSN: cyanide@ccode.de
  Mit Zitat antworten Zitat
Benutzerbild von Aenogym
Aenogym

Registriert seit: 7. Mär 2004
Ort: Schwerin
1.089 Beiträge
 
Delphi 7 Enterprise
 
#2

Re: boolean ohne variable?

  Alt 14. Dez 2004, 21:44
hi,

zwei gegenfargen: woher hast du das? und kompiliert es?

Aenogym
Steffen Rieke
Was nicht buzzt, wird buzzend gemacht!
http://blog.base-records.de
http://www.base-records.de
  Mit Zitat antworten Zitat
Benutzerbild von glkgereon
glkgereon

Registriert seit: 16. Mär 2004
2.287 Beiträge
 
#3

Re: boolean ohne variable?

  Alt 14. Dez 2004, 21:46
ja, es kompiliert....

aber wundern tuts mich auch
»Unlösbare Probleme sind in der Regel schwierig...«
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#4

Re: boolean ohne variable?

  Alt 14. Dez 2004, 21:52
es ist ein stück code den ich von SCP habe, ich versuche gerade den nach und nach nachzuvollziehn und scheitere bereits wie man sieht an den records ^^

ja es lässt sich kompilieren, wenn ich ein "end;" ans case ranfügen würde, würde es nichtmehr gehen.
ich kann zwar ein b: vor dem "boolean" machen, aber das war nur als test, es lässt sich mit und ohne kompilieren, nur versteh ich nicht wie dieser bool dann funktioniert?
Mario
MSN: cyanide@ccode.de
  Mit Zitat antworten Zitat
Benutzerbild von Aenogym
Aenogym

Registriert seit: 7. Mär 2004
Ort: Schwerin
1.089 Beiträge
 
Delphi 7 Enterprise
 
#5

Re: boolean ohne variable?

  Alt 14. Dez 2004, 22:00
so,

delphi-hilfe bemüht:

Zitat:
A record type can have a variant part, which looks like a case statement. The variant part must follow the other fields in the record declaration.

To declare a record type with a variant part, use the following syntax.

Delphi-Quellcode:
type recordTypeName = record
   fieldList1: type1;
     ...
   fieldListn: typen;
   case tag: ordinalType of
   constantList1: (variant1);
     ...
   constantListn: (variantn);
   end;
The first part of the declaration--up to the reserved word case--is the same as that of a standard record type. The remainder of the declaration--from case to the optional final semicolon--is called the variant part. In the variant part,
  • tag is optional and can be any valid identifier. If you omit tag, omit the colon (:) after it as well.
  • ordinalType denotes an ordinal type.
  • Each constantList is a constant denoting a value of type ordinalType, or a comma-delimited list of such constants. No value can be represented more than once in the combined constantLists.
  • Each variant is a semicolon-delimited list of declarations resembling the fieldList: type constructions in the main part of the record type. That is, a variant has the form

    Code:
    fieldList1: type1;
        ...
       fieldListn: typen;
    where each fieldList is a valid identifier or comma-delimited list of identifiers, each type denotes a type, and the final semicolon is optional. The types must not be long strings, dynamic arrays, variants (that is, Variant types), or interfaces, nor can they be structured types that contain long strings, dynamic arrays, variants, or interfaces; but they can be pointers to these types.

Records with variant parts are complicated syntactically but deceptively simple semantically. The variant part of a record contains several variants which share the same space in memory. You can read or write to any field of any variant at any time; but if you write to a field in one variant and then to a field in another variant, you may be overwriting your own data. The tag, if there is one, functions as an extra field (of type ordinalType) in the non-variant part of the record.

Variant parts have two purposes. First, suppose you want to create a record type that has fields for different kinds of data, but you know that you will never need to use all of the fields in a single record instance. For example,

Delphi-Quellcode:
type
  TEmployee = record
  FirstName, LastName: string[40];
  BirthDate: TDate;
  case Salaried: Boolean of
    True: (AnnualSalary: Currency);
    False: (HourlyWage: Currency);
end;
The idea here is that every employee has either a salary or an hourly wage, but not both. So when you create an instance of TEmployee, there is no reason to allocate enough memory for both fields. In this case, the only difference between the variants is in the field names, but the fields could just as easily have been of different types. Consider some more complicated examples:

Delphi-Quellcode:
type
  TPerson = record
  FirstName, LastName: string[40];
  BirthDate: TDate;
  case Citizen: Boolean of
    True: (Birthplace: string[40]);
    False: (Country: string[20];
            EntryPort: string[20];
            EntryDate, ExitDate: TDate);
  end;
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;
For each record instance, the compiler allocates enough memory to hold all the fields in the largest variant. The optional tag and the constantLists (like Rectangle, Triangle, and so forth in the last example) play no role in the way the compiler manages the fields; they are there only for the convenience of the programmer.

The second reason for variant parts is that they let you treat the same data as belonging to different types, even in cases where the compiler would not allow a typecast. For example, if you have a 64-bit Real as the first field in one variant and a 32-bit Integer as the first field in another, you can assign a value to the Real field and then read back the first 32 bits of it as the value of the Integer field (passing it, say, to a function that requires integer parameters).
:D

Aenogym

edit: das nächste mal verwende ich die vorschau :roll:
Steffen Rieke
Was nicht buzzt, wird buzzend gemacht!
http://blog.base-records.de
http://www.base-records.de
  Mit Zitat antworten Zitat
paresy

Registriert seit: 24. Aug 2004
Ort: Lübeck
105 Beiträge
 
Delphi 2007 Professional
 
#6

Re: boolean ohne variable?

  Alt 14. Dez 2004, 22:11
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  fs : TFileStream;
  DataSize : Word;
  DataType : Word;
  CurEntry : record
              Header : record
               Tag : array[0..3] Of Char;
               EntryType : Word;
               Size : Word;
              end;
              case boolean of
               true: (Value : DWORD; Junk: array[0..1023] of Char);
               false : (Str : array[0..1023] of Char);
             end;
  CurInfo : String;
  CurJunk : array[0..1023] Of Char;
begin
  //...
end;
damit kannst du in einem record mehrere variablen im selben speicher halten -> das ganze dient der übersicht wenn du ein record für 2 sachen verwenden willst und dadurch 2 ( hier 2 durch das boolean -> true/false ) verschieden bezeichnungen brauchst.

beispiel:

Delphi-Quellcode:
 TIPSVarType = (vtBoolean, vtInteger, vtFloat, vtString);
 TIPSVarTypes = set of TIPSVarType;

 PIPSVariable = ^TIPSVariable;
 TIPSVariable = record
  VarName : PChar;
  VarType : TIPSVarType;
  VarInfo : PChar;
  VarUpdated : TDateTime;
  case TIPSVarType of
   vtBoolean : (VarBoolean : Boolean);
   vtInteger : (VarInteger : Integer);
   vtFloat : (VarFloat : Single);
   vtString : (VarString : PChar);
 end;
hier habe ich für jeden variablentyen einen eigenen namen mit verschiedenen typen (Boolean/Integer..). da das record je immer nur eine variabe annehmen kann muss ich ja nicht alle möglichkeiten im record gleichzeitig aufnehmen sondern nehme per case nur jeweils eins rein was mich an speicher nur soviel kostet wie der größte typ ( der compiler denkt da halt mit... und mir erspart es arbeit bzw arbeitsspeicher )

werd mal bei gelegenheit das topic aus der delphi help suchen.... da gabs was...
  Mit Zitat antworten Zitat
Benutzerbild von dizzy
dizzy

Registriert seit: 26. Nov 2003
Ort: Lünen
1.932 Beiträge
 
Delphi 7 Enterprise
 
#7

Re: boolean ohne variable?

  Alt 14. Dez 2004, 22:12
[ot]
"Junk" ist ein schöner Bezeichner . Das heisst so viel wie Müll/Schrott. "Chunk" ist mir in Verbindung mit binären Dateiformaten eher ein Begriff (heisst ~Stück, im Sinne z.B. von "a chunk of meat")
[/ot]

Jö, wollt ich nur am Rande los werden

btw: DAS nenne ich mal nen Record! Record in Record, und dann noch mit variantem Teil und (verkappten) Strings. Is ja alles drin so
Fabian K.
INSERT INTO HandVonFreundin SELECT * FROM Himmel
  Mit Zitat antworten Zitat
Benutzerbild von Pseudemys Nelsoni
Pseudemys Nelsoni

Registriert seit: 24. Dez 2002
Ort: Hamburg-Harburg
3.551 Beiträge
 
#8

Re: boolean ohne variable?

  Alt 14. Dez 2004, 22:21
@Aenogym:

wenn ich keinen bezeichner habe für den boolean, wie spreche ich ihn dann an?

@paresy:

ich weiss schon wie variante records funktionieren, mir ist nur schleierhaft wieso da nach dem case kein "end;" kommt
Mario
MSN: cyanide@ccode.de
  Mit Zitat antworten Zitat
paresy

Registriert seit: 24. Aug 2004
Ort: Lübeck
105 Beiträge
 
Delphi 2007 Professional
 
#9

Re: boolean ohne variable?

  Alt 14. Dez 2004, 22:26
Zitat von Pseudemys Nelsoni:
@Aenogym:

wenn ich keinen bezeichner habe für den boolean, wie spreche ich ihn dann an?
garnicht.... der compiler bietet dir alle möglichkeiten an. das boolean bringt nur dass du im case 2 möglichkeiten hast.
  Mit Zitat antworten Zitat
Benutzerbild von dizzy
dizzy

Registriert seit: 26. Nov 2003
Ort: Lünen
1.932 Beiträge
 
Delphi 7 Enterprise
 
#10

Re: boolean ohne variable?

  Alt 14. Dez 2004, 22:28
Ich darf?

Zitat von Pseudemys Nelsoni:
@Aenogym:
wenn ich keinen bezeichner habe für den boolean, wie spreche ich ihn dann an?
Garnicht. Du wählst die passende Variante allein durch das Schreiben des Elementbezeichners. Das "case" hier ist bloß ein sprachliches Konstrukt, und funktioniert nicht wie "case" im eigentlichen Code.

Zitat von Pseudemys Nelsoni:
@paresy:
ich weiss schon wie variante records funktionieren, mir ist nur schleierhaft wieso da nach dem case kein "end;" kommt
Weil der variante Teil ohnehin immer der letzte im Record sein muss. Somit ist der variante Teil genau dann zu ende, wenn der Record zu ende ist
Fabian K.
INSERT INTO HandVonFreundin SELECT * FROM Himmel
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 4  1 23     Letzte »    


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:20 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz