2012-06-11 5 views
2

사전을 매개 변수로 전달하고 싶습니다. 내 코드는 조금 생소 할 수 있지만, 어쨌든 그것을 보여주지 :사전을 매개 변수로 전달하십시오.

Sub get_Insurers_Dictionary() 

Dim cInsurers    As c_Insurers 
Dim myDictionary   As Scripting.Dictionary 

Set cInsurers = New c_Insurers 

Set myDictionary = New Scripting.Dictionary 
Set myDictionary = cInsurers.Get_Parameters_Dictionary(cInsurers, myDictionary) 

End Sub 

을 그 다음 c_Insurers 클래스 :

겉으로는 복잡한 코드에서 제외
Public Function Get_Parameters_Dictionary(cInsurers As c_Insurers, myDictionary As Dictionary) As Dictionary 

Dim oSheet      As Excel.Worksheet 
Dim cParameters     As c_Parameters 


Set oSheet = ThisWorkbook.Sheets("Parameters_Insurers") 
oSheet.Activate 

Set cParameters = New c_Parameters 
Set cParameters = cParameters.Get_Parameters(cParameters, oSheet) 

Me.fPartner_ID_Col = cParameters.get_Header_Cols(oSheet, cParameters.fMax_Col, "INSURER_ID") 
Me.fPartner_Name_Col = cParameters.get_Header_Cols(oSheet, cParameters.fMax_Col, "INSURER_NAME") 
Me.fCountry_Col = cParameters.get_Header_Cols(oSheet, cParameters.fMax_Col, "COUNTRY") 

For lcnt = 1 To UBound(cParameters.fArray) 
    myDictionary.Add cParameters.fArray(lcnt, Me.fPartner_ID_Col), Me.Get_Parameters_Class(cInsurers, cParameters, lcnt) 
Next lcnt 

Set Get_Parameters_Insurers = myDictionary 
Set cParameters = Nothing 

End Function 

, 내 질문은 간단하다 클래스 객체를 항목으로 추가 할 수 있습니까? (나는 아직 사전을 사용하는 데 익숙하지 않다.) 내 사전은 c_insurers 클래스에서 채워지지만 get_Insurers_Dictionary로 돌아 가면 왜 비어 있습니까?

답변

1

사전을 Get_Parameters_Dictionary에 전달했기 때문에 예상대로 작동하지 않지만 채워진 후에는 결코 반환하지 않고 대신 다른 것으로 할당 할 수 있습니다. Set Get_Parameters_Insurers = myDictionary.

즉, Set myDictionary = cInsurers.Get_Parameters_Dictionary(cInsurers, myDictionary)myDictionary에서 Nothing으로 재설정됩니다 (할당되지 않은 반환 유형이기 때문에).

Set Get_Parameters_Dictionary = myDictionaryGet_Parameters_Dictionary() 중 하나 여야합니다.

Set myDictionary = New Scripting.Dictionary 
cInsurers.Get_Parameters_Dictionary cInsurers, myDictionary 

myDictionaryGet_Parameters_Dictionary에서의 변경을 반영 할 것이다 : 후 있도록 사전 참조 형식이다.

+0

예, 실수로 눈치 챘습니다. "Get_Parameters_Insurers"의 이름을 "Get_Parameters_Dictionary"로 변경했습니다. 나는 당신이 둥근 괄호 사이에 "cInsurers, myDictionary"를 의미한다고 생각하니? 그리고 또 다른 문제가 있습니다. set cInsurers = myDictionary.Item (20) (예)를 수행하여 올바른 클래스 인스턴스를 검색 할 수 없습니다. 그것은 나에게 틀린 클래스 인스턴스, 즉 사전에 마지막으로 추가 된 인스턴스를 돌려 준다. 왜 그럴 수 있는지? 많은 감사합니다! – Trace

+1

인수를 사용하여 함수를 호출하지만 반환 값을 캡처하지 않으려면 대괄호를 사용하지 마십시오 (호출 앞에'Call func (arg) '를 붙이면됩니다). 다른 호출은 대개 ​​참조를 다시 사용하기 때문에 가능합니다 그래서 같은 일을 20 번 더하는 것이 사실입니다. http://stackoverflow.com/questions/2674341/excel-vba-passing-a-collection-from-a-class-to-a-module-issue –

+0

젠장, 고마워, 그게 다야. 동일한 c_Insurers 클래스 객체를 다시 사용하고 있는데, 얼마나 바보 같은가. 매우 중요한 질문입니다. 사전에 추가 한 후 c_Insurers 객체를 릴리스 할 수 있습니까? 즉, 생성되는 객체의 사본입니까? 스택 과부하 위험없이 사전에 얼마나 많은 객체를 저장할 수 있습니까? – Trace

관련 문제