?
Solved

How to add handlers to events for a late-bound object?

Posted on 2006-07-11
17
Medium Priority
?
631 Views
Last Modified: 2008-01-09
I have a .NET dll which is being referenced in a VB.NET application.

I'm referencing this object using late binding as we want the application to cope if the dll is missing from the run directory. This will happen in certain cases as not everyone who runs the application will need or have access to the dll. Please don't tell me to just give everyone the dll, that's not what we want.

The problem is that one of the objects I referencing from the dll raises a few events which I want to handle in the application.

I've defined the object variable like so:
Private m_objOptions As Object

and I'm initialising it like so:
m_objOptions = New Compass.clGetOptions()

I'm trying to add a handler to one of the events like so:
AddHandler m_objOptions.Status, AddressOf Message

but I'm getting the error
'Status' is not an event of 'System.Object'

Can anyone help me resolve this?
How do I add a handler to a late-bound object?

Many Thanks,
Janine
0
Comment
Question by:janineo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 2
  • +2
17 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17080501
You'll have to look at the Compass object... but the error seems to indicate that you are not using a valid event name in your AddHandler statement. I'd guess you need something like StatusChanged.
AddHandler m_objOptions.StatusChanged, AddressOf Message

Is Compass your own class?
0
 

Author Comment

by:janineo
ID: 17080652
Compass is my own class, and the event is called Status.

The AddHandler line compiles with no problem when I use early binding, i.e I declare the object like so:
Private m_objOptions As Compass.clGetOptions

0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 1400 total points
ID: 17080761
Try Typecasting

AddHandler CType(m_objOptions, Compass.clGetOptions).Status, AddressOf Message

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17080777
There's the question of why, exactly, you're late-binding that control. Having to guess, I'd say that it is because you have several classes with a status event, but want to use them interchangably at run-time based on certain settings?
0
 
LVL 5

Expert Comment

by:vinodhsomasekharan
ID: 17080795
why do u want to use the late binding?

Private m_objOptions As Object
m_objOptions = New Compass.clGetOptions()

The above code is not a latebinding code.

You know the type of the object is "Compass.clGetOptions" then why are u declare "Private m_objOptions As Object" instead of "Private m_objOptions As Compass.clGetOptions"?

Private m_objOptions As Compass.clGetOptions
the above line is just a declaration.

We can create a latebinded object in the following ways

Private m_objOptions As Compass.clGetOptions
m_objOptions = DirectCast( CreateObject("Compass.clGetOptions") , Compass.clGetOptions)

or
Private m_objOptions As Compass.clGetOptions
m_objOptions = DirectCast( Activator.CreateInstance(gettype("Compass.clGetOptions")) , Compass.clGetOptions)

then add the event handler

regards
vinu
 

0
 

Author Comment

by:janineo
ID: 17081054
arif_eqbal: Using CType didn't work - the IDE complained about needing a singular object with AddHandler.

Chaosian: Not quite. What we're doing is an experiment. We want to create a sort of 'Add-in' for the application. If the dll is in the run directory then certain options are available in the application, and if it's not then the user never knows they are missing.

vinodhsomasekharan: I want to do late binding as using early binding - declaring objOptions as Compass.clGetOptions causes errors when the dll is missing from the run directory, which I want to handle gracefully.
The error I get is
File or assembly name Compass, or one of its dependencies, was not found.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17081451
I don't think you can do this.  Although you can add to an Object's properties and methods by late-binding it to a specific subordinate class - e.g.

    Private m_objOptions As Object

        m_objOptions = "abcd"
        'property
        MsgBox(m_objOptions.length)
        'method
        Dim s As String = m_objOptions.substring(0, 1)
        MsgBox(s)

- so far as I can tell it remains stuck with the events dictated by its original declaration.

I'll be happy to be proved wrong, but ...

Roger
0
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 600 total points
ID: 17081473
Okay.... why not declare the object as the correct type, and only add the handler if the conditions are met? This gets you around the late-binding issue.

Roger... interfaces :)
0
 

Author Comment

by:janineo
ID: 17081678
Roger,

Thanks, but that didn't work.
The form didn't open, probably because the m_objOptions variable is a class-level variable and defining it as something that it can't find killed the form.
I'm getting the same 'File or assembly name Compass, or one of its dependencies, was not found.' error.

0
 

Author Comment

by:janineo
ID: 17081687
Sorry, I meant Chaosian, not Roger!
0
 

Author Comment

by:janineo
ID: 17081875
I've been googling the problem, and I've seen references to using OLE Callback.
I have no idea what this is, or if it's suitable in my case.
Can anyone enlighten me to this?
0
 

Author Comment

by:janineo
ID: 17082285
I've just found out that using CType did work - as far as compilation anyway. I must have typed it wrongly before.

I think I may not be late-binding properly - In VB6 I would use CreateObject, I'm guessing that it's different in VB.NET?
Not just assigning an variable of type Object to some other value?

I'm still getting the 'File or assembly name Compass, or one of its dependencies, was not found.' error.
It may have to do with a control from the library that is created on the form. How would I late-bind this?
0
 

Author Comment

by:janineo
ID: 17082924
Okay, I think I'm getting there.

I've defined an Object at the class level, and then used
Activator.CreateInstance()
to create an instance of the control.

However I'm still getting the 'assembly not found' error during the method where I'm adding an event handler to the control using
AddHandler CType(myCtl, Compass.usrControl).Status, AddressOf myCtl_Status

So I think I'm almost back to my original problem!
I can get it to compile now, but it doesn't run properly if the dll is missing.
Maybe I can just try and catch the error, though I'm not sure how.
I'm getting the JIT error box even though I've got a try-catch in the method.

I'll think on it more tomorrow.
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 17087740
Well, Typecasting should work with AddHandler
If you can resolve the 'File or Assembly not found' issue it would hopefully work
Your assembly "Compass" does it refer any other DLL/Assembly ?
Check if the dependencies of the referenced assembly are fine
Check if the required files are available in the path that the Runtime looks into for locating the files
This will help you know where all the runtime looks for dependencies
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconhowruntimelocatesassemblies.asp

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17087753
I still wonder if you wouldn't be better to instantiate it as a specific type only if the client has purchased the appropriate module...you'd avoid all the late binding... which, in this case,seems like a waste of effort since you only ever bind to one type anyhow.

Or did I miss something?
0
 

Author Comment

by:janineo
ID: 17088603
Chaosian,

It's an initial experiment. Our company is part of a multinational organisation and here in the UK we've created an application that is used by the UK branch.

The UK has just been merged into a 'UK, Europe, and Africa' group and our IT director is talking about 'exporting' our application into Europe & Africa.

However, we don't want to give them our source code for them to mess around with, and equally we don't want to build and support a different version of the app for each country, as each countries apps will have different requirements.

So we're looking into breaking the app down into wholely enclosed sections that we can package up as dlls and send out to the other countries for them to integrate without really knowing much about them.

We're also considering building our own 'IDE' for creating the finished app, hence the 'ignore the section if the dll isn't there' issue I'm having. It's entirely possible that the people doing the 'building' will have very little programming knowledge - like excel macros or something.

As I said, it's all an experiment - can we do this, should we do this, will it be more hassle than it's worth?
0
 

Author Comment

by:janineo
ID: 17088751
It works!

Would you believe that moving the code that only gets executed if the dll is there into it's own function and calling that instead stopped the unhandled exception from occuring?

It's exactly the same code, called in exactly the same place, it's just wrapped up in a different function.

Well, that's VB.NET for you. Must have something to do with how it does the compilation.

I'll split the points between arif_eqbal and Chaosian as you both helped with your comments, though the type casting did solve the original issue.

Many Thanks,
Janine

0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question