Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

Passing a dotnet control as generic object problem???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nazimuddin Tajuddin Basha - 26 Apr 2004 08:45 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.