VB to .NET

WARNING #1048

Add a delegate for AddressOf 1%

Description

This EWI is emitted when a function pointer is used in the original Visual Basic 6 code. In .NET, this is an unmanaged operation and has been replaced by delegates.

Recommendations

Define a delegate for the function and use it instead of using the Long address values.

Sample VB6

PublicDeclareFunction SetTimer Lib"user32" _
   (ByVal hwnd AsLong, _
   ByVal nIDEvent AsLong, _
   ByVal uElapse AsLong, _
   ByVal lpTimerFunc AsLong) AsLong

PublicFunction UsingAddressOf() AsInteger
   Dim TimerId AsLong
   TimerId = SetTimer(0, 0, 500, AddressOf TestSub)
   UsingAddressOf = TimerId
EndFunction

PublicFunction TestSub(ByVal Message AsString) AsBoolean
   If Message <> ""Then
      MsgBox(Message)
      TestSub = True
   Else
      TestSub = False
   EndIf
EndFunction

Target VB.NET

PublicDeclareFunction SetTimer Lib"user32" (ByVal hwnd AsInteger, ByVal nIDEvent AsInteger, ByVal uElapse AsInteger, ByVal lpTimerFunc AsInteger) AsInteger

PublicFunction UsingAddressOf() AsInteger
   'UPGRADE_WARNING: (1048) Add a delegate for AddressOf TestSub
   Dim TimerId AsInteger = SetTimer(0, 0, 500, AddressOf TestSub())
   Return TimerId
EndFunction

PublicFunction TestSub(ByRef Message AsString) AsBoolean
   If Message <> ""Then
      MessageBox.Show(Message, Application.ProductName)
      ReturnTrue
   Else
      ReturnFalse
   EndIf
EndFunction

Expected VB.NET

:

PublicDeclareFunction SetTimer Lib"user32" (ByVal hwnd AsInteger, ByVal nIDEvent AsInteger, ByVal uElapse AsInteger, ByVal lpTimerFunc As DelegateOfFunction) AsInteger

PublicDelegateFunction DelegateOfFunction(ByVal Message AsString) AsBoolean

PublicFunction UsingAddressOf() AsInteger
   Dim TimerId AsInteger = SetTimer(0, 0, 500, TestSub)
   Return TimerId
EndFunction

Target C#

DllImport("user32.dll")]
externpublicstaticint SetTimer( int hwnd, int nIDEvent, int uElapse, int lpTimerFunc);

staticpublicint UsingAddressOf()
{
   //UPGRADE_WARNING: (1048) Add a delegate for AddressOf TestSub
   int TimerId = SetTimer(0, 0, 500, TestSub());
   return (int) TimerId;
}

staticpublicbool TestSub( string Message)
{
   if (Message != "")
   {
      MessageBox.Show(Message, Application.ProductName);
      returntrue;
   }
   else
   {
      returnfalse;
   }
}

Expected C#

[DllImport("user32.dll")]

externpublicstaticint SetTimer(int hwnd, int nIDEvent, int uElapse, DelegateOfFunction lpTimerFunc);

publicdelegatebool DelegateOfFunction(string message);

staticpublicint UsingAddressOf()
{
   int TimerId = SetTimer(0, 0, 500, TestSub);
   return (int) TimerId;
}

Talk To An Engineer