在不同的Delphi函数中使用对象

Ort*_*x92 -2 delphi class object delphi-2010

这只是一个非常简单的问题,我无法找到一个明确的答案.我没有时间阅读所有文档,因为我正处于紧张状态.

但在这里.

我在我的TForm类之上创建了一个新类,如下所示:

 Bucket = Class
   glass: Integer;
   steel: Integer;
 End;
Run Code Online (Sandbox Code Playgroud)

然后我在一个属于TForm1的方法中创建了几个对象

procedure TForm1.getMarbles;
var
  objPlastic: Bucket;
  objAlu: Bucket;

begin
  // Initialize objects
  objPlastic := Bucket.Create;
  objAlu := Bucket.Create;

  // Get Values from edtBox
  val(Edit1.Text, objPlastic.steel, code);
  val(Edit2.Text, objAlu.steel, code);
  val(Edit3.Text, objPlastic.glass, code);
  val(Edit4.Text, objAlu.glass, code);
end; 
Run Code Online (Sandbox Code Playgroud)

我的问题是我不知道如何在其他方法中使用这些对象.到目前为止,我尝试用我想要使用的其他方法中的各种方式来定义它们,但是我无法让它工作.

这是方法和我当前设置的方法(它始终返回0):

procedure TForm1.marbleDrop(kind: string);
var
  objPlastic: Bucket;
  I: Integer;
begin
  objPlastic := Bucket.Create;
  if kind= 'plastic' then // the function is receiving this parameter
  begin
    for I := 0 to objPlastic.glass do
    begin
      showmessage(inttostr(objPlastic.glass)); //returns 0
    end;
  end;

end;
Run Code Online (Sandbox Code Playgroud)

对不起,这个问题,但我找不到更好的方法.

顺便说一句,这是我正在使用的代码的简化版本.我尽力摆脱任何错别字,因为它是我实际使用的翻译,但它主要是关于这个想法.我在delphi中的代码中没有拼写错误.

Rem*_*eau 5

另一方面,要跨方法访问对象,您必须:

  1. 将对象声明为Form类的成员:

    type
      TForm1 = class(TForm);
      ...
      private
        objPlastic: Bucket;
        objAlu: Bucket;
      ...
      end;
    
    procedure TForm1.getMarbles;
    begin
      // Initialize objects
      if objPlastic = nil then objPlastic := Bucket.Create;
      if objAlu = nil then objAlu := Bucket.Create;
    
      // Get Values from edtBox
      objPlastic.steel := StrToIntDef(Edit1.Text, 0);
      objAlu.steel := StrToIntDef(Edit2.Text, 0);
      objPlastic.glass := StrToIntDef(Edit3.Text, 0);
      objAlu.glass := StrToIntDef(Edit4.Text, 0);
    end;
    
    procedure TForm1.marbleDrop(kind: string);
    begin
      if (kind = 'plastic') and (objPlastic <> nil) then
      begin
        ShowMessage(IntToStr(objPlastic.glass));
      end;
    end;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将它们作为方法本身的参数传递:

    procedure TForm1.getMarbles(objPlastic, objAlu: Bucket);
    begin
      // Get Values from edtBox
      if objPlastic <> nil then
      begin
        objPlastic.steel := StrToIntDef(Edit1.Text, 0);
        objPlastic.glass := StrToIntDef(Edit3.Text, 0);
      end;
      if objAlu <> nil then
      begin
        objAlu.steel := StrToIntDef(Edit2.Text, 0);
        objAlu.glass := StrToIntDef(Edit4.Text, 0);
      end;
    end;
    
    procedure TForm1.marbleDrop(objWhichKind: Bucket);
    begin
      if objWhichKind <> nil then
      begin
        ShowMessage(IntToStr(objWhichKind.glass));
      end;
    end;
    
    procedure TForm1.someMethod();
    var
      objPlastic: Bucket;
    begin
      objPlastic := Bucket.Create;
      getMarbles(objPlastic, nil);
      marbleDrop(objPlastic);
      objPlastic.Free;
    end;
    
    Run Code Online (Sandbox Code Playgroud)