Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

procedure SetInfo(MyObj:TObject; PropName: string; const Value: TValue); var PropKind:…

  1. User

    procedure Set_Info(MyObj:TObject; PropName: string; const Value: TValue); var PropKind: TTypeKind; Ctx: TRttiContext; Field: TRttiField; begin Ctx := TRttiContext.Create; try Field := Ctx.GetType(MyObj.ClassType).GetField('F' + PropName); if Assigned(Field) then Field.SetValue(MyObj, Value) else raise Exception.Create('Error: Field "PropName + '" not found.'); finally // TRttiContext is a record; no need to free end; end;

    procedure Set_Info_NullableDateTime(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;

    procedure Set_Info_NullableExtended(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;

    procedure Set_Info_NullableInteger(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;

    procedure Set_Info_NullableInt64(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;

    procedure Set_Info_NullableString(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;

    procedure Set_Info_NullableVariant(MyObj:TObject; PropName: string; const Value: TNullable); begin Set_Info(MyObj, PropName, TValue.From<TNullable>(Value)); end;


    unit pixel_classes;

    interface

    uses System.SysUtils, Rtti, TypInfo;

    {$TYPEINFO ON}

    type TNullValue = record end;

    TNullable<T{: record}> = record public Value: T; HasValue: Boolean;

    class operator Implicit(const AValue: T): TNullable<T>;
    class operator Implicit(const AValue: TNullable<T>): T;
    class operator Implicit(const AValue: TNullValue): TNullable<T>;
    class operator Explicit(const AValue: T): TNullable<T>;
    
    // add these...
    class operator Equal(const A: TNullable<T>; const B: TNullValue): Boolean;
    class operator NotEqual(const A: TNullable<T>; const B: TNullValue): Boolean;
    

    end;

    var NullValue: TNullValue;

    implementation

    class operator TNullable.Implicit(const AValue: T): TNullable; begin Result.Value := AValue; Result.HasValue := True; end;

    class operator TNullable.Implicit(const AValue: TNullable): T; begin if AValue.HasValue then Result := AValue.Value else Result := Default(T); // or raise an exception end;

    class operator TNullable.Implicit(const AValue: TNullValue): TNullable; begin Result.Value := Default(T); Result.HasValue := False; end;

    class operator TNullable.Explicit(const AValue: T): TNullable; begin Result.Value := AValue; Result.HasValue := True; end;

    class operator TNullable.Equal(const A: TNullable; const B: TNullValue): Boolean; begin Result := not A.HasValue; end;

    class operator TNullable.NotEqual(const A: TNullable; const B: TNullValue): Boolean; begin Result := A.HasValue; end;

    end.


    Review teh code above and create a complete example of usage for the following class:

    type TEmployeeProject = class private FEmployeeID: TNullable; FDepartmentID: TNullable; FEmployeeName: TNullable; FProjectID: TNullable; FProjectName: TNullable; FProjectBudget: TNullable; FProjectDate: TNullable; FProjectStartMoment: TNullable;

    published property EmployeeID: TNullable read FEmployeeID write FEmployeeID; property DepartmentID: TNullable read FDepartmentID write FEmployeeID; property EmployeeName: TNullable read FEmployeeName write FEmployeeID; property ProjectID: TNullable read FProjectID write FEmployeeID; property ProjectName: TNullable read FProjectName write FEmployeeID; property ProjectBudget: TNullable read FProjectBudget write FEmployeeID; property ProjectDate: TNullable read FProjectDate write FEmployeeID; property ProjectStartMoment: TNullable read FProjectStartMoment write FEmployeeID; end;


  2. Assistant

    To create a complete usage example of the TEmployeeProject class with the Set_Info procedures defined earlier, you can define an application or unit where you're creating an instance of TEmployeeProject and using the Set_Info procedures to set the properties. Here's an example demonstrating this:

    program EmployeeProjectExample;
    
    uses
      System.SysUtils, System.Rtti, pixel_classes;
    
    type
      TEmployeeProject = class
      private
        FEmployeeID: TNullable<Integer>;
        FDepartmentID: TNullable<Integer>;
        FEmployeeName: TNullable<String>;
        FProjectID: TNullable<Integer>;
        FProjectName: TNullable<String>;
        FProjectBudget: TNullable<Currency>;
        FProjectDate: TNullable<TDateTime>;
        FProjectStartMoment: TNullable<TDateTime>;
    
      published
        property EmployeeID: TNullable<Integer> read FEmployeeID write FEmployeeID;
        property DepartmentID: TNullable<Integer> read FDepartmentID write FDepartmentID;
        property EmployeeName: TNullable<String> read FEmployeeName write FEmployeeName;
        property ProjectID: TNullable<Integer> read FProjectID write FProjectID;
        property ProjectName: TNullable<String> read FProjectName write FProjectName;
        property ProjectBudget: TNullable<Currency> read FProjectBudget write FProjectBudget;
        property ProjectDate: TNullable<TDateTime> read FProjectDate write FProjectDate;
        property ProjectStartMoment: TNullable<TDateTime> read FProjectStartMoment write FProjectStartMoment;
      end;
    
    procedure DisplayProjectInfo(Project: TEmployeeProject);
    begin
      Writeln('Employee ID: ', Project.EmployeeID.Value);
      Writeln('Department ID: ', Project.DepartmentID.Value);
      Writeln('Employee Name: ', Project.EmployeeName.Value);
      Writeln('Project ID: ', Project.ProjectID.Value);
      Writeln('Project Name: ', Project.ProjectName.Value);
      Writeln('Project Budget: ', FormatCurr('0.00', Project.ProjectBudget.Value));
      Writeln('Project Date: ', DateTimeToStr(Project.ProjectDate.Value));
      Writeln('Project Start Moment: ', DateTimeToStr(Project.ProjectStartMoment.Value));
    end;
    
    var
      Project: TEmployeeProject;
    begin
      try
        Project := TEmployeeProject.Create;
    
        // Setting the values using Set_Info procedures
        Set_Info_NullableInteger(Project, 'EmployeeID', 101);
        Set_Info_NullableInteger(Project, 'DepartmentID', 10);
        Set_Info_NullableString(Project, 'EmployeeName', 'John Doe');
        Set_Info_NullableInteger(Project, 'ProjectID', 1234);
        Set_Info_NullableString(Project, 'ProjectName', 'AI Research');
        Set_Info(Project, 'ProjectBudget', TValue.From<TNullable<Currency>>(50000.0));
        Set_Info_NullableDateTime(Project, 'ProjectDate', EncodeDate(2023, 10, 20));
        Set_Info_NullableDateTime(Project, 'ProjectStartMoment', Now);
    
        // Displaying the project information
        DisplayProjectInfo(Project);
      finally
        Project.Free;
      end;
    
      Readln;
    end.
    

    Explanation:

    1. Project Initialization: We create an instance of TEmployeeProject and allocate it to the variable Project.

    2. Property Setting: Using Set_Info and its overloaded versions like Set_Info_NullableInteger, we set values to each field. For Currency, we use TValue.From<TNullable<Currency>>.

    3. DisplayProjectInfo: A helper procedure to display all properties for the instance we have created and modified.

    4. Cleaning up: Finally, Project.Free ensures any allocated memory or resources are properly released.

    This example assumes that the Set_Info procedure is generic enough to handle TNullable<Currency> similarly to other types and includes exception handling for missing fields or incompatible type errors. Additionally, be sure to compile and run this code in a Delphi environment that supports Generics and RTTI (Run Time Type Identification), which are essential parts of this implementation.