Upgrade to VB 2008?

parkins
11-03-2009, 04:32 PM
Re: http://www.xtremevbtalk.com/showthread.php?t=275204
All,

I am trying to run this code in VB 2008 to see how it works (I'm learning) and I am getting an error that says:

UPGRADE_WARNING: Add a delegate for AddressOf AppBarProc

Plus a "Click for More..." that made my head hurt.

Can someone help me understand Delegates and what to do here?

Thanks
Please do not reply to threads over 30 days old and start your own threads to ask your own questions. I have created a link to the original thread and moved this post to the .NET forum. Welcome to the forum!

AtmaWeapon
11-04-2009, 09:29 AM
I'd be careful when reusing VB6 examples that use API calls in VB .NET. There are some differences between how types are used for API calls in VB6 and VB .NET, and getting something wrong doesn't always cause an error that makes it clear what happened.

For example, Long was a 32-bit integer in VB6 but is a 64-bit integer in VB .NET; any API call that used Long as a parameter in VB6 must use Integer in VB .NET. But it's not that simple. Sometimes Long is used to represent a handle, in which case you should use .NET's IntPtr.

If I were you I'd look at a few of the C# examples linked in this thread (http://www.xtremevbtalk.com/showthread.php?t=293359) so you'd be starting with a .NET example. As I pointed out in that thread, I fiddled with this API for a while and ultimately decided it was pretty difficult to get everything right. Be very careful!

To answer your question, a delegate is a variable that holds a method. This can be useful for many things. When working with the API, it's usually used for callback functions; these used function pointers in C/C++. It's a lot easier to *show* a delegate than it is to describe it, so have a look at this:
Module Module1

' This is the delegate declaration. It reads like this:
' "Create a delegate type named DoSomething that can represent any method that takes a string
' parameter and returns no value."
Private Delegate Sub DoSomething(ByVal input As String)

Sub Main()
' This is a delegate variable. It can store a reference to any method that matches the
' signature of DoSomething.
Dim printMethod As DoSomething

Dim text As String = "MeSsEd Up CaPs"

' AddressOf is used so the VB compiler knows you don't want to call PrintUpper but instead
' want a reference to it. You can store this reference in the delegate variable.
printMethod = AddressOf PrintUpper
' You call the method stored in a delegate variable by calling its Invoke() method.
printMethod.Invoke(text)

printMethod = AddressOf PrintLower
' VB also lets you treat a delegate variable like the method it represents. This
' automatically calls Invoke().
printMethod(text)
End Sub

' This matches the DoSomething delegate signature: no return value and a string parameter.
Private Sub PrintUpper(ByVal input As String)
Console.WriteLine(input.ToUpper())
End Sub

' This matches the DoSomething delegate signature: no return value and a string parameter.
' Note that the parameter name doesn't have to match.
Private Sub PrintLower(ByVal text As String)
Console.WriteLine(text.ToLower())
End Sub

End Module

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum