スパムちゃんです。
使い回しを聞かせるために、特定の関数を外部ファイル化したいのですが、、、
unit MyFuncでユニットを作りました。
procedure に関数を作り、使うunitで、使用unitの追加。
で、
MyFnc.Create;
Myfnc.使う関数
してじゃないとダメなのでしょうか?
いろいろなところで使う関数なので、FormCreate内で、MyFnc.Createして、終了させる時に、解放するって手順でいいのでしょうか?
他にもっと簡単な方法はありますか?
Form1から、Form2のは、Createしなくても使えますよね?
Form2.使う関数();
その違いは何でしょうか?
> 使い回しを聞かせるために、特定の関数を外部ファイル化したいのですが、、、
> Myfnc.使う関数
これで十分。
uses にユニット名を書いてあれば、使う関数 だけで使うことができます。
> Form1から、Form2のは、Createしなくても使えますよね?
フォームが自動生成されているため、使えるだけかな?
やはりそうですよね。
Myfnc.使う関数では、使えないんですよ。
未定義の識別子 : '関数名'となってしまうのです。orz
きちんと、外部化したunitの関数も、unit1のunitに追加してるし、外部化した関数の関数もPublicにしてるんです。
なんでだろー?w?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
uses MyShow;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
s : TMyShow;
begin
//以下だとok。
s := TMyShow.Create;
s.ShowM();
s.Free;
//下だと、関数が見つからないみたい。
//下の方法だとダメなんでしょうか?
TMyShow.ShowM();
end;
end.
***外部unit*************************
unit MyShow;
interface
uses Dialogs;
type
TMyShow = class(TObject)
private
{ Private 宣言 }
public
{ Public 宣言 }
procedure ShowM();
end;
implementation
uses Unit1;
procedure TMyShow.ShowM();
begin
ShowMessage('ZZZ');
end;
end.
すいません、何が違うのか、アホすぎて分かりません。^^;
procedure ShowM()をclassのメンバーにする必要がないのなら、
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
uses MyShow;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowM
end;
end.
//外部関数******
unit MyShow;
interface
uses Dialogs;
procedure ShowM();
implementation
procedure ShowM();
begin
ShowMessage('ZZZ');
end;
end.
シンプルにこれで良いのでは?
クラスメソッドにしなさい。
procedure TForm1.Button1Click(Sender: TObject);
var
s : TMyShow;
begin
s := TMyShow.Create;
s.ShowM('生成して呼び出し');
s.Free;
TMyShow.ShowM('生成しないで呼び出し');
end;
-----------------------------------------
unit MyShow;
interface
uses Dialogs;
type
TMyShow = class(TObject)
private
{ Private 宣言 }
public
{ Public 宣言 }
class procedure ShowM(const ss: string); // クラスメソッド
end;
implementation
class procedure TMyShow.ShowM(const ss: string);
begin
ShowMessage(ss);
end;
end.
よもや、クラスにしない方法だったり、classを付けるだけだったとは。
もう、恥ずかしくって、死んじゃいたいくらいです。orz
「このメソッドの呼び方はクラスメソッドの場合に限られます」ってエラーが出たので、それに関して調べていたのですが・・・、基本が分かっていないのか、難しい説明がよく分からないのか、よく分かりませんでした。
みなさん、アホアホな、スパムちゃんの質問に、ありがとうございました。
(〃⌒∇⌒)/。・:*:・°★,。・:*:・°☆アリガトー!
ツイート | ![]() |