Click to See Complete Forum and Search --> : indexed property


kakalake
December 19th, 2008, 07:30 AM
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

MadHatter
December 19th, 2008, 08:01 AM
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.

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
}
}

kakalake
December 19th, 2008, 08:09 AM
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

darwen
December 19th, 2008, 09:00 AM
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 :


[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.

kakalake
December 19th, 2008, 09:41 AM
Thanks again for your reply :-)

My code look like this:


private List<double> SearchXVals = new List<double>();
public System.Collections.IList SearchX
{
get
{
return SearchXVals;
}
}


and I get the same error....

darwen
December 19th, 2008, 10:02 AM
Try using the 'Item' method in VB e.g.


Dim xx As ...Example;
Dim d As Double;
Set d = xx.SearchX.Item(3);


Darwen.

kakalake
December 22nd, 2008, 05:07 PM
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

darwen
December 22nd, 2008, 05:14 PM
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.

kakalake
January 19th, 2009, 03:56 AM
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:


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


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 :-)