API Declaration Styles

G

Guest

There are two styles of declaring external API functions in VB:

1. VB6-Style (for lack of a better description)
Public Declare ...

2. Using attributes (similar to C#) and omitting the function body:
<DLLImport ....>
Public Shared Function ...
End Function

I've always assumed that the latter style was more in the spirit of .NET and
our Instant VB converter currently converts to the latter style, but recent
googling seems to show that the first style is still very popular. Any
thoughts on the two different styles?
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#
 
H

Herfried K. Wagner [MVP]

David Anton said:
There are two styles of declaring external API functions in VB:

1. VB6-Style (for lack of a better description)
Public Declare ...

2. Using attributes (similar to C#) and omitting the function body:
<DLLImport ....>
Public Shared Function ...
End Function

I've always assumed that the latter style was more in the spirit of .NET
and
our Instant VB converter currently converts to the latter style, but
recent
googling seems to show that the first style is still very popular. Any
thoughts on the two different styles?


I prefer VB.NET's 'Declare' syntax over 'DllImport' because I think it's
more self-documenting and easier to use due to better IntelliSense support.
'Declare' statements don't need the 'Shared' modifier when placed in a
class, while 'DllImport' functions do. In addition to that, many declares
can already be found in the 'Declare' format. Still, there are some cases
when 'DllImport' must.
 
D

Dragon

According to Lutz Roeder's .NET Reflector
(http://www.aisto.com/roeder/dotnet), Declare statements are converted
to "attributish" style, for example

~
Friend Declare Auto Function LoadLibrary Lib "kernel32.dll" ( _
<MarshalAs(UnmanagedType.LPTStr)> ByVal LibFileName As String _
) As IntPtr
~

becomes

~
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Friend Shared Function LoadLibrary(<MarshalAs(UnmanagedType.LPTStr)>
ByVal LibFileName As String) As IntPtr
End Function
~

To my mind, classic VB-style declarations are no bad (since they are not
considered as obsolete like On Error statements), because they take less
space and are easier to type (IMHO).
 
G

Guest

Thanks Herfried, Dragon, and Mattias - I think we'll offer an option to
convert to the 'Declare' format when the attributes in the original C# code
being convered are appropriate.

Thanks also for the link Mattias!

--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top