Immutable optional parameters

M

Marco Segurini

Hi,

Actually I am calling from VB some functions imported from a c++ dll.
Some of these functions require as parameter a pointer.
Example:

extern "C" int DllCall(int * pvInt, int iLen);

Now in VB I call this function passing a single element and/or an array.
To do this I define two alias function:

Public Declare Function DllCall_1 Lib "ImportDll.dll" Alias "DllCall"
(ByRef i as Integer, Optional ByVal nCount As Integer = 1) As Integer

Public Declare Function DllCall_n Lib "ImportDll.dll" Alias "DllCall"
(ByVal vInt() As Integer, ByVal nCount As Integer) As Integer

Sample of calling code:

Dim i as integer = 4
Call DllCall_1(i,1)
Dim vInt(10) as Integer
Call DllCall_n(vInt, 11)

The second parameter for 'DllCall_1' must be always 1
so I like to know if there is a way to declare the optional
parameter as 'immutable' so

Call DllCall_1(i) automatically becomes Call DllCall_1 (i,1)
and
Call DllCall_1 (i,1) -> build error
Call DllCall_1 (i,3) -> build error

Thanks.
Marco.

PS: I know that this can be reach with a wrapper function like

function WrapDllCall_1(ByRef i as Integer) as Integer
return DllCall(i,1)
End Function
 
P

Phill. W

.. . .
if there is a way to declare the optional parameter as 'immutable'

If the argument is always the same, why have it as an Optional
Argument in the first place? Oh well ...

The closest you can probably get is to redefine the argument as an
Enum; at least that will stop Developers coding anything else, as in

Public Enum AlwaysOne_E
ValueOfOne = 1
End Enum

Public Declare Function DllCall_1 Lib "ImportDll.dll" Alias "DllCall" _
( ByRef i As Integer _
, Optional ByVal nCount As AlwaysOne_E = ValueOfOne _
) As Integer

HTH,
Phill W.
 
J

Jay B. Harlow [MVP - Outlook]

Phill,
However you can still call the function passing any value you want!

Unlike Pascal, .NET does not enforce the value you place in an Enum, as long
as it fits in the underlying Integer, Short or Long, you can use any numeric
amount you want!
Public Declare Function DllCall_1 Lib "ImportDll.dll" Alias "DllCall" _
( ByRef i As Integer _
, Optional ByVal nCount As AlwaysOne_E = ValueOfOne _
) As Integer

DllCall_1(1, CType(55, AlwaysOne_E))

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Marco,
PS: I know that this can be reach with a wrapper function like

function WrapDllCall_1(ByRef i as Integer) as Integer
return DllCall(i,1)
End Function
If you know that the second parameter has to be one, the only way to ensure
this is to use a wrapper as you suggest.

Hope this helps
Jay
 

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