|
-
December 19th, 2008, 08:30 AM
#1
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
-
December 19th, 2008, 09:01 AM
#2
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
}
}
-
December 19th, 2008, 09:09 AM
#3
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
-
December 19th, 2008, 10:00 AM
#4
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.
-
December 19th, 2008, 10:41 AM
#5
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....
-
December 19th, 2008, 11:02 AM
#6
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.
-
December 22nd, 2008, 06:07 PM
#7
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
-
December 22nd, 2008, 06:14 PM
#8
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.
-
January 19th, 2009, 04:56 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|