Using C# or VB.NET collection in your VB6 Applications

28. August 2008 05:34 by Mrojas in General  //  Tags: , , , ,   //   Comments (0)

If you have some .NET code that you want to share with VB6, COM has always been a nice option. You just add couple of ComVisible tags and that's all.


Collections can be a little tricky.

This is a simple example of how to expose your Collections To VB6.

Here I create an ArrayList descendant that you can use to expose your collections.
Just create a new C# class library project and add the code below.
Remember to check the Register for ComInterop setting.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CollectionsInterop
    public interface IMyCollectionInterface 

        int Add(object value);
        void Clear();
        bool Contains(object value);
        int IndexOf(object value);
        void Insert(int index, object value);
        void Remove(object value);
        void RemoveAt(int index);

        System.Collections.IEnumerator GetEnumerator();
        object this[int index]

    public class VB6InteropArrayList : System.Collections.ArrayList, IMyCollectionInterface

        #region IMyCollectionInterface Members

        // COM friendly strong typed GetEnumerator

        public System.Collections.IEnumerator GetEnumerator()
            return base.GetEnumerator();


    /// <summary>
    /// Simple object for example 
    /// </summary>
    public class MyObject
        String value1 = "nulo";

        public String Value1
            get { return value1; }
            set { value1 = value; }
        String value2 = "nulo";

        public String Value2
            get { return value2; }
            set { value2 = value; }

To test this code you can use this VB6 code. Remember to add a reference to this class library.
Private Sub Form_Load()
    Dim simpleCollection As New CollectionsInterop.VB6InteropArrayList
    Dim value As New CollectionsInterop.MyObject
    value.Value1 = "Mi valor1"
    value.Value2 = "Autre valeur"
    simpleCollection.Add value
    For Each c In simpleCollection
      MsgBox value.Value1
End Sub