首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

Undeclared identifier: 'YearOf'有关问题

2012-08-27 
Undeclared identifier: YearOf问题程序如下:unit MainForminterfaceusesWindows, Messages, SysUtils,

Undeclared identifier: 'YearOf'问题
程序如下:
unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Mask, StdCtrls;

type
  TForm1 = class(TForm)
  lblName: TLabel;
  Label2: TLabel;
  Label3: TLabel;
  Label4: TLabel;
  Label5: TLabel;
  edtName: TEdit;
  edtScore1: TEdit;
  edtScore2: TEdit;
  meIDCard: TMaskEdit;
  mePhone: TMaskEdit;
  btnGetInfo: TButton;
  lblBirthday: TLabel;
  lblSex: TLabel;
  lblAge: TLabel;
  lblScore: TLabel;
  procedure edtScore1KeyPress(Sender: TObject; var Key: Char);
  procedure edtScore2KeyPress(Sender: TObject; var Key: Char);
  procedure btnGetInfoClick(Sender: TObject);
  private
  { Private declarations }
  function ExtractBirthday(const IDCard:string):TDate;
  function ExtractSex(const IDCard:string):Boolean;
  function CalcScore(const S1,S2:string):Real;
  public
  { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.ExtractBirthday (const IDCard:string):TDate;
var
  Str:string;
  Len:Integer;
begin
  Str:=Trim(IDCard);
  Len:=Length(IDCard);
  case Len of
  15:Str:=Copy(Str,7,2)+'-'+Copy(Str,9,2)+'-'+Copy(Str,11,2);
  18:Str:=Copy(Str,7,4)+'-'+Copy(Str,11,2)+'-'+Copy(Str,13,2);
  end;
  Result:=StrToDate(Str);
end;
function TForm1.ExtractSex (const IDCard:string):Boolean;
var
  Str:string;
  Len:Integer;
begin
  Result:=False;
  Str:=Trim(IDCard);
  Len:=Length(Str);
  case Len of
  15:Result:=StrToInt(Str[15]) mod 2<>0;
  18:Result:=StrToInt(Str[17]) mod 2<>0;
  end;
end;
function TForm1.CalcScore (const S1,S2:string):Real;
begin
  Result:=StrToInt(S1)*0.6+StrToInt(S2)*0.4;
end;
procedure TForm1.edtScore1KeyPress(Sender: TObject; var Key: Char);
begin
  if(key<#8)or((key>#8) and (key<#46)) or ((key>#46)and(key<#48)) or (key>#57) then
  Key:=#0;
end;

procedure TForm1.edtScore2KeyPress(Sender: TObject; var Key: Char);
begin
  if(key<#8)or((key>#8) and (key<#46)) or ((key>#46)and(key<#48)) or (key>#57) then
  Key:=#0;
end;

procedure TForm1.btnGetInfoClick(Sender: TObject);
var
  Birthday:TDate;
begin
  Birthday:=ExtractBirthday(meIDCard.Text);
  if Length(Trim(meIDCard.Text))>0 then begin
  lblBirthday.Caption:=Format('出生日期:%d年%d月%d日',[YearOf(Birthday),MonthOf(Birthday),DayOf(Birthday)]);
  if ExtractSex(meIDCard.Text) then
  lblSex.Caption:='性别:男'
  else
  lblSex.Caption:='性别:女';
  end;
  lblAge.Caption:='年龄:'+IntToStr(YearOf(Now)-YearOf(Birthday));
  lblScore.Caption:='考试成绩:'+Format('%5.1f',[CalcScore(edtScore1.Text,edtScore2.Text)])'
end;

end.
运行后提示错误为:
[Error] MainForm.pas(93): Undeclared identifier: 'YearOf'
  [Error] MainForm.pas(93): Undeclared identifier: 'MonthOf'
  [Error] MainForm.pas(93): Undeclared identifier: 'DayOf'
  [Error] MainForm.pas(100): Unterminated string
  [Fatal Error] Project1.dpr(5): Could not compile used unit '..\chenyue\Exa04-14\MainForm.pas'

[解决办法]
uses DateUtils;
------解决方案--------------------


引用单元 uses DateUtils
[解决办法]
lblScore.Caption:='考试成绩:'+Format('%5.1f',[CalcScore(edtScore1.Text,edtScore2.Text)])'

把最后的单引号去掉!
[解决办法]
第100行,把后面的引号去掉

lblScore.Caption:='考试成绩:'+Format('%5.1f',[CalcScore(edtScore1.Text,edtScore2.Text)])'


热点排行