Solved

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

Posted on 2006-07-11
17
616 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
  • 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 350 total points
ID: 17080761
Try Typecasting

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

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 57
VB.net Open Folder in Windows Explorer 3 29
get column names from table in vb.net 8 30
Store results in vb.net 3 22
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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