2010-12-15 1 views
3

C#에서 개체를 만드는 방법 :VBA.Collection()</p> <p>것은 내가 그 전화있을 때 나는 <br> 내가 내 프로젝트에 Interop.VBA 참조를 C# 코드에서 Interop를 VBA.Collection 개체를 만들 필요가

http://support.microsoft.com/kb/323737/en-us

그것은 작동 할 수 있지만 내 상자에 VB6 컴파일러가 없습니다 :

var col = new VBA.Collection() 

런타임에서 나는
나는 것을 발견 ... 그 DLL이 등록되지 않은 말하는 오류를 가지고있다.
다른 해결 방법을 알고 계신지 궁금하신 점이 있으십니까?

+1

그 페이지는 "Visual Basic 6.0 응용 프로그램 만 VBA.Collection 클래스의 인스턴스를 만들 수 있습니다"라고 말합니다. 무엇을 위해 컬렉션이 필요합니까? 아마도 당신이 성취하려고하는 또 다른 방법이있을 것입니다. – Foole

+0

VBA 용으로 작성된 DLL을 사용하고 있습니다. 소유지 중 하나를 관리해야하는 이유 모음 유형 – Maciej

답변

4

이 방법을 사용하지 않았지만 제대로 작동하지 않을 수 있습니다.

VB6의 VBA6.dll에 대한 가져 오기 라이브러리를 만듭니다. _Collection 인터페이스의 자체 구현을 작성하십시오. 이 구현을 VBA.Collection 클래스 대신 사용하십시오.

class MyCollection : VBA._Collection 
{ 
    private Dictionary<object, object> _items = new Dictionary<object, object>(); 

    public void Add(ref object Item, [System.Runtime.InteropServices.OptionalAttribute]ref object Key, [System.Runtime.InteropServices.OptionalAttribute]ref object Before, [System.Runtime.InteropServices.OptionalAttribute]ref object After) 
    { 
     // Ignoring the Before and After params for simplicity 
     _items.Add(Key, Item); 
    } 

    public int Count() 
    { 
     return _items.Count; 
    } 

    public System.Collections.IEnumerator GetEnumerator() 
    { 
     return _items.Values.GetEnumerator(); 
    } 

    public dynamic Item(ref object Index) 
    { 
     return _items[Index]; 
    } 

    public void Remove(ref object Index) 
    { 
     _items.Remove(Index); 
    } 
} 
+0

당신은 그 사람입니다! 해로 일합니다! Add()의 작은 변경 키 및 항목 매개 변수가 대체됩니다 (첫 번째 키가 Key임을 나타냄) – Maciej

0

나는 이것을 vb.net에 맞게 수정했으며 누락되면 null이기 때문에 add에 오는 키를 고쳐야 만했다.

테스트 후이 게시물을 편집합니다. VB6에서 .Net dll을 호출하고 vba 컬렉션을 매개 변수로 전달하고 .Net dll이 반환 값으로 다른 vba 컬렉션을 다시 전달할 때 작동하는지 확인해야합니다. 이봐,이게 효과가 있다면 나에게 많은 어려움을 줄거야!

Public Class VBACollection 
    Implements VBA._Collection 

    Private _items As New Dictionary(Of Object, Object) 

    Public Sub Add(ByRef Item As Object, Optional ByRef Key As Object = Nothing, Optional ByRef Before As Object = Nothing, Optional ByRef After As Object = Nothing) Implements VBA._Collection.Add 
    ' Ignoring the Before and After params for simplicity 
    Key = If(Key, Item) 
    _items.Add(Key, Item) 
    End Sub 

    Public Function Count() As Integer Implements VBA._Collection.Count 
    Return _items.Count 
    End Function 

    Public Function GetEnumerator() As System.Collections.IEnumerator Implements VBA._Collection.GetEnumerator, System.Collections.IEnumerable.GetEnumerator 
    Return _items.Values.GetEnumerator() 
    End Function 

    Public Function Item(ByRef Index As Object) As Object Implements VBA._Collection.Item 
    Return _items(Index) 
    End Function 

    Public Sub Remove(ByRef Index As Object) Implements VBA._Collection.Remove 
    _items.Remove(Index) 
    End Sub 
End Class 

편집 :

아니,이 VB6 작동하지 않습니다. VB6는 말한다 :

는 여기에 대해서 이야기 대신 VBA.Collection의 VBACollection를 사용하여 내 수업이 클래스를 "클래스가 자동화를 지원하지 않거나 예상 인터페이스를 지원하지 않습니다." VBACollection은 VBA.Collection과 동일한 스탠드 인이 아닙니다. 왜 그런지 알아 내고 COM을 속여서 받아들이려고합니다.

관련 문제