Hi All,
I have written a VB activex dll component, which exposes a method say
MethodA that takes one of the arguments as object (generic) type. The
method looks like this:
Public Sub MethodA(ByRef ctrl As Object, ByVal categoryID As
FontCategoriesEnum, Optional ByVal resID As Long)
When i use this dll within the VB projects, works fine. When i use
this in dotnet, i have to convert the control as object type and pass
it. But it gives me InvalidCastException......anybody have come across
or have the answer for this, plz drop in few lines??. Bcoz this is
very imp step in my project.
Idael Cardoso - 27 Apr 2004 01:21 GMT
The problem is that Control or one of its ancestors has defined the
ComVisibleAttribute as false, that's why the COM wrapper doesn't supply a
valid dispatch wrapper for the methods or properties of the control. You can
do two things. Defines helper class that wraps the method and properties
that you need to manage in your VB code (if they are not too much). Or
derive all the controls you need to pass to your COM object and apply to
these control the ComVisibleAttribute, settting it to true.
Suppose that in your VB code you have the following code:
Public Sub YourMethod(ByVal obj As Object)
Label1.Caption = obj.Text
obj.SelectedIndex = 0
End Sub
This method expects a combo box control, take its text value and later set
its selected index as the first in its list.
To use this method you can do the following:
public class ComboWrapper
{
private ComboBox m_Wrapped;
public ComboWrapper(ComboBox w)
{
m_Wrapped = w;
}
public string Text
{
get { return m_Wrapped.Text;
}
set
{
m_Wrapped.Text = value;
}
}
public int SelectedIndex
{
get
{
return m_Wrapped.SelectedIndex;
}
set
{
m_Wrapped.SelectedIndex = value;
}
}
}
in any of your forms you have defined:
private System.Windows.Forms.ComboBox comboBox1;
then you call you VB method as follows:
.. YourVbCtrl.YourMethod(new ComboWrapper(comboBox1));
The other solution is define the following:
[ComVisible(true)]
public class MyComboBox : ComboBox
{
public MyComboBox():base()
{
}
and change of:
comboBox1 = new ComboBox() ;
by
comboBox1 = new MyComboBox() ;
then you can call the VB method like this:
YourVbCtrl.YourMethod(comboBox1);
I prefer the wrapper version: could be more secure and give a much better
performance.
Best regards,
Idael.
> Hi All,
>
[quoted text clipped - 10 lines]
> or have the answer for this, plz drop in few lines??. Bcoz this is
> very imp step in my project.