I have this code in my unit:
unit clsDate_u;
interface
type
TDate = class(TObject)
private
fDay, fMonth, fYear : integer;
public
constructor Create(sDate: string);
function MonthName: string;
function DaysPassedInYear: integer;
function LongDate: string;
function GetDay: integer;
function GetMonth: integer;
function GetYear: integer;
end;
implementation
{ TDate }
uses
SysUtils, DateUtils;
constructor TDate.Create(sDate: string);
begin
fDay := StrToInt(copy(sDate,1,2));
fMonth := StrToInt(copy(sDate,4,2));
fYear := StrToInt(copy(sDate,7,2));
if fYear <= 29 then
fYear := fYear 2000
else
fYear := fYear 1900;
end;
function TDate.DaysPassedInYear: integer;
var
iTotal, iCount: integer;
begin
iTotal := 0;
iCount := 1;
while iCount < fMonth do
begin
iTotal := iTotal DaysInAMonth(fYear, iCount);
Inc(iCount);
end;
iTotal := iTotal fDay;
result := iTotal;
end;
function TDate.GetDay: integer;
begin
result := fDay;
end;
function TDate.GetMonth: integer;
begin
result := fMonth;
end;
function TDate.GetYear: integer;
begin
result := fYear;
end;
function TDate.MonthName: string;
begin
result := LongMonthNames[fMonth];
end;
function TDate.LongDate: string;
begin
result := IntToStr(fDay) ' ' MonthName ' ' IntToStr(fYear);
end;
end.
I want to use the "LongMonthNames" variable in my function as you can see here:
function TDate.MonthName: string;
begin
result := LongMonthNames[fMonth];
end;
Delphi says it is an undeclared identifier and sees it as an error, but I did in fact add SysUtils and DateUtils in my uses clause.
Why does it not recognize "LongMonthNames"?
I am working in a seperate unit, but I doubt that has anything to do with the issue.
CodePudding user response:
First, TDate
is a poor name since it collides with the standard Delphi TDate
type.
Second, the LongMonthNames
thing you are looking for is a member of the TFormatSettings
record. Consequently, to access this member, you need an instance of such a record.
For instance, if you want localized month names, you can use TFormatSettings.Create
to create a format settings record for the current locale.
On the other hand, if you want non-localized (English) month names, you can use the TFormatSettings.Invariant
class function.
For example,
var LMonthNames := TFormatSettings.Create.LongMonthNames;
for var LMonthName in LMonthNames do
ShowMessage(LMonthName);
gives you localized month names while
var LMonthNames := TFormatSettings.Invariant.LongMonthNames;
for var LMonthName in LMonthNames do
ShowMessage(LMonthName);
gives you non-localized month names.
(Obviously, in real code you would not recreate the format settings record every time.)
CodePudding user response:
All I had to do was add "FormatSettings." before the "LongMonthNames", like this:
function TDate.MonthName: string;
begin
result := FormatSettings.LongMonthNames[fMonth];
end;