Results 1 to 13 of 13

Thread: Visual Basic “AddressOf expression cannot be converted to long”

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    4

    Question 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.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    4

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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?

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Visual Basic “AddressOf expression cannot be converted to long”

    Quote Originally Posted by dilettante View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Visual Basic “AddressOf expression cannot be converted to long”

    Quote Originally Posted by dday9 View Post
    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.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Visual Basic “AddressOf expression cannot be converted to long”

    Quote Originally Posted by dilettante View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Visual Basic “AddressOf expression cannot be converted to long”

    Quote Originally Posted by dilettante View Post
    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.

  12. #12
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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
  •  



Click Here to Expand Forum to Full Width