CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    indexed property

    Hello,

    is there a possibility to create in c# a indexed property that can be used in visual basic 6? I already tried to implement a indexer class object that implements the this indexer but this doesn't work.

    Please Help

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: indexed property

    you can't create an indexed property (other than the this property), but you can have a property that is an array, dictionary or any other collection, and would therefore be accessible with an index.

    Code:
    public class Foo {
        List<int> values = new List<int>;
    
        public List<int> Values {
            get { return values; }
        }
    }
    
    public class Program {
    
        public static void Main() {
            Foo f = new Foo();
            f.Values.Add(11);
            int x = f.Values[0]; // x will be 11
        }
    }

  3. #3
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    Re: indexed property

    Hello,

    first thanks for your reply. I tried this, but visual basic 6 is not able to invoke the indexed property. I get the Error "Wrong number of argument or unknown property assignment" (I translated this from german :-))

    Thanks

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: indexed property

    I'm not very surprised this doesn't work - generics don't translate to COM.

    Try returning a System.Collections.IList (i.e. non-generic) like this instead :

    Code:
    [Guid("0264699d-0113-43db-84fc-8e40ee12a04e")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IExample
    {
        System.Collections.IList List { get; }
    }
    
    [Guid("730e035e-fa90-47ec-b0b8-c23710b96b1b")]
    public class Example : IExample
    {
        List<int> _list;
    
        public Example()
        {
            _list = new List<int>();
            _list.Add(1);
            _list.Add(2);
            _list.Add(3);
        }        
    
        #region IExample Members
    
        public System.Collections.IList List
        {
            get { return _list; }
        }
    
        #endregion
    }
    I've tried accessing this in native C++ via COM and it works ok for me so it should work as required in VB6.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    Re: indexed property

    Thanks again for your reply :-)

    My code look like this:

    Code:
            private List<double> SearchXVals = new List<double>();
            public System.Collections.IList SearchX
            {
                get
                {
                    return SearchXVals;
                }
            }
    and I get the same error....

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: indexed property

    Try using the 'Item' method in VB e.g.

    Code:
    Dim xx As ...Example;
    Dim d As Double;
    Set d = xx.SearchX.Item(3);
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    Re: indexed property

    Hi again,

    the problem is that I cannot change the vb-code. It is part of a plugin architecture that was implemented and developed some time ago and there are a lot of plugins that use the already defined interface. So the only chance is to change the c# code to work with the vb-code. The other direction is not possible....

    Thanks again for your reply

  8. #8
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: indexed property

    You should have mentioned this in the first place...

    Do you have an example of a COM object which does work ? Examine its dll using OLE/COM viewer ("C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\OleView.exe") and post what interfaces the working COM objects implement.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  9. #9
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    Re: indexed property

    Hello darwen,

    first thanks a lot for all your replays and your efforts.
    Here is an example project that tries to access the indexed property of the c#-Com-module:

    Code:
    Private Sub Form_Load()
        On Error GoTo Fehler
    
        Dim object As Object
        Set object = CreateObject("IS_Gateway_COM_Plugin.IS_COMGatewayPlugin")
        object.SearchX(1) = 2#
        
        Exit Sub
    Fehler:
        MsgBox "Error: " & Error
        Unload Form1
    End Sub
    and here is my c# implementation

    Code:
            private List<double> SearchXVals = new List<double>();
            public System.Collections.IList SearchX
            {
                get
                {
                    return SearchXVals;
                }
            }
    But I always get the error: "Wrong number of arguments or invalid property assignment"

    Thanks again :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured