2015-09-05 67 views
-1

Delphi XE6。我創建了一個名爲TBizObject的類。這是一個非常簡單的課程。 (代碼片段在下面)。我有一個表格,帶有「測試」按鈕。Delphi - 在子程序內創建類實例的錯誤

procedure TMgtForm.Button1Click(Sender: TObject); 
var 
    BizObj1, BizObj2, BizObj3: TBizObj; 
begin 
// Test Routine 
    BizObj1 := TBizObj.Create; 
    BizObj2 := TBizObj.Create; 
    BizObj3 := TBizObj.Create; 
    CreateTest; 

    BizObj1.Free; 
    BizObj2.Free; 
    BizObj3.Free; 

end; 

ASSUME CreateTest被註釋掉了。代碼工作正常。隨着CreateTest我得到一個AV。 createTest例程只不過是Create和Free。

procedure TMgtForm.CreateTest; 
var 
    BizObj4: TBizObj; 
begin 

    BizObj4.Create; 
    BizObj4.Free; 
end; 

總之,我可以在一個主程序(即一鍵/菜單)創建我的課,但我不能有一個程序,調用一個子程序,它創建我的課。

當我深入瞭解錯誤時,我在fParenLine上獲得了AV。

// Constructor 
constructor TBizObj.Create; 
begin 
    inherited; 

    // Setup out default Values; 
    fParenList := TStringList.Create; 
    fUniqueWordList := TStringList.Create; 
    fUniqueWordList.Sorted := True; 
    fUniqueWordList.Duplicates := dupIgnore; 
    fBaseWordList := TStringList.Create; 
end; 

我的班級定義的相關部分如下。

type 
    TBizObj = class(TObject) 
    // Internal class field definitions - only accessible in this unit 
    private 
    fIncomingName: string; // This is the name that is passed in... 
    fIncomingNameLength: Integer; 
    ... 
    fParenList: TStringList; /// TStringList of ALL ParenthesisText, assuming last one will be City and State 
    ... 
    protected 
    // Externally accessible fields and methods 
    public 
    published 
    constructor Create; 
    destructor Destroy; override; 

應用程序編譯罰款...我可以創建許多BizObj我想要的,但只有在頂級例程。我究竟做錯了什麼?

感謝

+0

只是一個錯字。這種情況不時發生。 – TLama

回答

3
BizObj4.Create; 

這是錯誤的。像這樣創建一個實例:

BizObj4 := TBizObj.Create; 

注意在Button1ClickCreateTest代碼之間的差異。你在前者是正確的,但在後者中沒有。

+0

愚蠢的錯誤...我知道更好!謝謝。 – user1009073