菜鸟枚举问题
implementation
uses Math;
type
MyFont=(st,ls,ht);
var
ft:MyFont;
{$R *.dfm}
function ffont(fft:MyFont):string;
begin
case fft of
st:Result:= '宋体';
ls:Result:= '隶书';
ht:Result:= '黑体';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
If Button1.Caption = '宋体' then
begin
//ft:= st;
Edit1.Font.Name:= ffont(st);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
If Button2.Caption = '隶书' then
begin
//ft:= ls;
Edit1.Font.Name:= ffont(ls);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
If Button3.Caption = '黑体' then
begin
//ft:= ht;
Edit1.Font.Name:= ffont(ht);
end;
end;
end.
--------------
为什么不能去掉
var
ft:MyFont;
去掉就会提示错误。一定要指定一个枚举类型的变量吗,是不是语法就是这么规定的?
[解决办法]
不可能出错, 你新建一个工程, 用这几句试试
uses Math;
type
MyFont=(st,ls,ht);
{var
ft:MyFont;}
{$R *.dfm}
function ffont(fft:MyFont):string;
begin
case fft of
st:Result:= '宋体';
ls:Result:= '隶书';
ht:Result:= '黑体';
end;
end;
[解决办法]
uses
Math;
type
MyFont = (st, ls, ht);
function FFont(fft: MyFont): string;
begin
case fft of
st: Result := '宋体';
ls: Result := '隶体';
Ht: Result := '黑体';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(FFont(Ht));
end;
这样用得很好的啊!不用另外声明MyFont变量类型即可调用!
这个跟你原来得代码是一样的啊