|
-
Mar 10th, 2014, 02:33 PM
#1
Thread Starter
New Member
Visual Basic “AddressOf expression cannot be converted to long”
I am writing a code to control a portable radio in visual basic and have one single error that is AddressOf' expression cannot be converted to 'Long' because 'Long' is not a delegate type coming from the one line of code below.
Code:
ret = SETCALLBACK(comport, AddressOf IncomingMessageHandler)
which is part of this sub-routine
Dim ret As Byte
Dim comport As Byte
comport = 1 'Use com1
ret = SETCALLBACK(comport, AddressOf IncomingMessageHandler)
'ret = 0 for no error, 1 for com port in use, 2 for no com port
I have googled and read every result on the first 3 pages and cannot find anything that fixes this issue or makes any sense to me. I have been doing visual basic for some time so understand the basics.
Any help with be very much appreciated.
Last edited by russc155; Mar 10th, 2014 at 02:43 PM.
-
Mar 10th, 2014, 02:41 PM
#2
Re: Visual Basic “AddressOf expression cannot be converted to long”
Please you code tags. They look like this: [code][/code]
Basically AddressOf creates a delegate that points to the function by it's name. Typically you'll see AddressOf in AddHandler:
Code:
AddHandler Button1.Click, AddressOf Button1_Click
A sub named Button1_Click will be the sub that handles Button1's click event in that example. In your case, if IncomingMessageHandler is a function like this:
Code:
Private Function IncomingMessageHandler() As Long
Return 'Something
End Function
Then simply remove the AddressOf in the second parameter of your SETCALLBACK method:
Code:
ret = SETCALLBACK(comport, IncomingMessageHandler)
Otherwise, how does IncomingMessageHandler look like?
-
Mar 10th, 2014, 02:43 PM
#3
Re: Visual Basic “AddressOf expression cannot be converted to long”
Which language is this for? An address is generally a 32-bit item, which would be an Integer in .NET, but a Long in VB6.
What is SETCALLBACK? It looks like it might be taking a function pointer as an argument, so it seems like it might be a C/C++ dll call, but that's not clear.
My usual boring signature: Nothing
 
-
Mar 10th, 2014, 02:48 PM
#4
Thread Starter
New Member
Re: Visual Basic “AddressOf expression cannot be converted to long”
I am just going to try it with VB6 because the same code currently runs in word visual studio which is VB6 so I think the error may be down to that. Thanks Shaggy Hiker you gave me the brainwave 
I will reply if it is successful or not.
-
Mar 10th, 2014, 10:16 PM
#5
Re: Visual Basic “AddressOf expression cannot be converted to long”
I suspect what's happening is that you are using a VB6 declaration in VB.NET. What you should do is change the declaration of SETCALLBACK. In VB6 a function pointer is a Long but in VB.NET it's a delegate, so you need to change type of the second parameter.
-
Mar 11th, 2014, 11:59 AM
#6
Re: Visual Basic “AddressOf expression cannot be converted to long”
Thread moved to the VB6 forum.
How did you declare the SETCALLBACK function and do you have IncomingMessageHandler sub in a module?
-
Mar 11th, 2014, 12:29 PM
#7
Re: Visual Basic “AddressOf expression cannot be converted to long”
This sounds like an issue stemming from compatibility breaks in 64-bit Office VBA data types to me.
In any case I can't see a reason why this thread was moved here. VBA is not VB6, never was, and is even less so today.
-
Mar 11th, 2014, 12:35 PM
#8
Re: Visual Basic “AddressOf expression cannot be converted to long”
 Originally Posted by dilettante
This sounds like an issue stemming from compatibility breaks in 64-bit Office VBA data types to me.
In any case I can't see a reason why this thread was moved here. VBA is not VB6, never was, and is even less so today.
It was originally posted in the Visual Basic.Net forum.
-
Mar 11th, 2014, 12:40 PM
#9
Re: Visual Basic “AddressOf expression cannot be converted to long”
 Originally Posted by dday9
It was originally posted in the Visual Basic.Net forum.
Well the OP admitted that it is Office VBA (post #4) so it sounds like it needs to move to the correct forum instead.
-
Mar 11th, 2014, 01:26 PM
#10
Re: Visual Basic “AddressOf expression cannot be converted to long”
 Originally Posted by dilettante
Well the OP admitted that it is Office VBA (post #4) so it sounds like it needs to move to the correct forum instead.
Agreed.
-
Mar 11th, 2014, 02:07 PM
#11
Re: Visual Basic “AddressOf expression cannot be converted to long”
 Originally Posted by dilettante
This sounds like an issue stemming from compatibility breaks in 64-bit Office VBA data types to me.
In any case I can't see a reason why this thread was moved here. VBA is not VB6, never was, and is even less so today.
The question the OP asked is not related to Office, it's a pure VB question, regardless if he uses VBA or not.
-
Mar 11th, 2014, 02:18 PM
#12
Re: Visual Basic “AddressOf expression cannot be converted to long”
The question doesn't appear to have anything to do with "Visual Basic 6 and earlier" at all.
If anything he probably needs to be using LongPtr Data Type (which doesn't exist in Visual Basic 6.0) in his SETCALLBACK definition. However since this is not an Office VBA forum people here wouldn't know that, or care.
-
Mar 11th, 2014, 02:41 PM
#13
Re: Visual Basic “AddressOf expression cannot be converted to long”
So until the OP answers the question I posted to him we wont know, will we? The question and the code he posted have absolutely nothing to do with the DOM in any of the Office applications. We don't have a VBA forum anymore, we have this forum and the Office Development forum. He's not doing Office development regardless if he happens to use one of the code editors in an office application. The problem might very well be that his IncomingMessageHandler isn't in a module, you would get that exact error if you would try to call a function that accepts a callback (such as EnumWindows) from VB6 and have the callback function declared inside a Form.
Tags for this Thread
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
|