2012-11-02 8 views
3

저는 QTP를 처음 사용하고 방금 사용하기 시작했습니다. 나는 몇 가지 기능 라이브러리에 하나의 클래스 정의를 작성하고도 아래로 테스트를 만들었습니다QTP의 관련 라이브러리가 작동하지 않습니다.

Class ExcelFileReader 
Public default Function Init(pathToExcel) 
    Dim objFSO 
    Dim result 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 

    If objFSO.FileExists(pathToExcel) Then 
      Rem File Found 
      Dim objExcel 
      Set objExcel = CreateObject("Excel.Application") 
      objExcel.Workbooks.open(pathToExcel) 

     Else 
      REM File not found 
      result = vbOk 
      While result <> vbCancel 
       result = Msgbox ("Unable to Locate the file", 5, "Error") 
      Wend 
      ExitAction(1) 
     End If 
End Function 

최종 클래스

테스트 : 나는 시험과 기능 라이브러리를 연결 한

Dim objExcelReader : Set objExcelReader = New ExcelFileReader 
objExcelReader.Init("D:\mytest.xlsx") 

하지만, 여전히 클래스 정의를 찾을 수 없다는 테스트에서 2 번 줄에 오류가 발생합니다. 또한 동일한 파일 "test"에 전체 코드를 복사하면 문제가 의도 한대로 작동합니다.

미리 감사드립니다. :)

답변

6

클래스에 라이브러리의 로컬 범위가 있습니다.

Public Function new_ExcelFileReader() 
    Set new_ExcelFileReader = new ExcelFileReader 
End Function 

Class ExcelFileReader 
    Sub Class_Initialize 
     MsgBox "Present!" 
    End Sub 
End Class 

그리고 다른 라이브러리에

:

Dim objExcelReader : Set objExcelReader = New_ExcelFileReader 
objExcelReader.Init("D:\mytest.xlsx") 

Protip을 : 당신은 그들을 공개적으로 사용할 수 있도록 공공 기능을 구성 할 필요가 당신은 당신의 생성자 함수에 초기화 매개 변수를 전달할 수 있습니다.

편집 요청에

: 생성자의 매개 변수를 전달하는 방법에 대해 설명합니다. 가끔 같은 개체가 내 구현에서는

Public Function new_ExcelFileReader2(filepath, sheetname) 
    Set new_ExcelFileReader2 = new ExcelFileReader 
    new_ExcelFileReader2.Init(filepath, sheetname) 
End Function 

' And the call: 
Set myExcelFileReader = new_ExcelFileReader2("C:\temp\tempExcel.xlsx", "sheet1") 

을하지만 여러 생성자 함수에서 '구성'됩니다 : 그냥 생성자 함수에 추가합니다. 귀하의 경우에 new_ExcelFileReader, new_CSVFileReadernew_TabDelimitedReader이 모두 동일한 객체를 가리키고 다르게 구성 될 수 있습니다.

코드를 멋지게 만드는 또 다른 방법은 init 함수로 객체 (me 키워드 사용)를 반환하는 것입니다. 이것은 다음과 같은 코드가 발생합니다 : 당신은 단지 객체를 반환하고 Init 함수를 호출하여 사용할 수 있습니다 생성자 함수와

Class ExcelFileReader 

    private filepath_ 
    public function Init(filepath) 
     filepath_ = filepath 
     Set Init = me 
    end function 

End Class 

Set myExcelFileReader = new ExcelFileReader.Init("C:\temp\tmpExcel.xlsx") 

.

Public Function new_ExcelFileReader() ' this is the same as the first function 
    Set new_ExcelFileReader = new ExcelFileReader 
End Function 

Set myExcelFileReader = new_ExcelFileReader.Init("C:\temp\tmpExcel.xlsx") 
+0

안녕 AutomatedChaos, 고마워요. 제안한대로 매개 변수화 된 생성자를 만드는 방법을 알려주시겠습니까? 시도했지만 시도 할 수 없었습니다. – codeomnitrix

+0

@AutomatedChaos 그는 수업을 공개로 설정할 수 없었습니까? – TheBlastOne

+0

@TheBlastOne 아니요, 함수, 하위 또는 변수로 할 수있는 것처럼 클래스를 public으로 설정할 수 없습니다 (예 :'Public Class YadaYada'). 적어도 QTP에는 없습니다. – AutomatedChaos

관련 문제