Solved

How do I make a Delphi primary type equal to TDatetime, but not automatically castable to TDatetime

Posted on 2014-11-06
5
384 Views
Last Modified: 2014-11-13
I have the following example unit. How do I make TSinceDateTime so that it will not compile the code inside "ErrorConvert" and successfully compile the code in "SuccessConvert" ?  Basically, I want to have a TDateTime type that is a primary type similar to TDateTime in that it can hold a timestamp, but I want the compiler to check that I don't mix those two in my source code. I would like to avoid making TSinceDateTime as a class, because then I have to instantiate that in all my already existing code.

unit MyUnit;

interface

uses SysUtils, Windows;

type
  TSinceDateTime = type Double;

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
function SuccessConvert(sdt : TSinceDateTime) : TDateTime;

implementation

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
begin
  Result := sdt; // Should not compile
end;

function SuccessConvert(sdt : TSinceDateTime) : TDateTime;
begin
  Result := TDateTime(sdt); // Should compile
end;

end.

Open in new window

0
Comment
Question by:lefodnes
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 40427903
So, you want that your code be compiled or not? Or you want to check if value sdt is real TDateTime?
not just plain Double? Variants can help in this....
0
 

Author Comment

by:lefodnes
ID: 40439779
I've requested that this question be deleted for the following reason:

Sorry. Bad explanation from my side, and now I found a better solution to my problem, which has nothing to do with this post.
0
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 40439663
My below code does exactly what you want, but I'm not sure, if it makes any sense!!!
If you remove the comment symbols/double-slash from line " //Result := sdt; // Should not compile", the compiler throws the error:
E2010 Incompatible Types: 'TDateTime' and 'TSinceDateTime'

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

  TSinceDateTime = record
    SinceDateTime: TDateTime;
  end;

var
  Form1: TForm1;

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
function SuccessConvert(sdt : TSinceDateTime) : TDateTime;


implementation

{$R *.dfm}

function ErrorConvert(sdt : TSinceDateTime) : TDateTime;
begin
   //Result := sdt; // Should not compile
end;

function SuccessConvert(sdt : TSinceDateTime) : TDateTime;
begin
  Result := TDateTime(sdt); // Should compile
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  sdtRec: TSinceDateTime;
begin
  ShowMessage(DateToStr(now));
  with sdtRec do SinceDateTime:=Now;
  ShowMessage(DateToStr(SuccessConvert(sdtRec)));
end;

end.

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 40439699
Additionally I'd like to provide you some considerations about turning off implicit type conversions and if it makes sense or not...

How to turn off implicit type conversions?
Designing code by contract
0
 

Author Closing Comment

by:lefodnes
ID: 40439780
Ahh!! That's clever :) Thanks a lot. Easy solution, still clever.
So now I'm able to just change the type of all those TDateTime's that I have, to TSinceDateTime, and I will get a warning on those parts of the application that still tries to use them as a TDateTime. I will also look into that link you provided. Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 67
Making delphi communicate with a c# service 16 90
TEMBEDDEDWB how can i change its user agent ? 8 49
Message not shown 5 33
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now