By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Function with Different Number of Arguments

P: 3
I have a client application which is using a specific library. This library can have different versions and I would like to have only one application flexible to be used with all different versions of this library.
My main problem now is that they changed the number of arguments of some specific functions in different versions and I don't know if I can workaround in VB for that.
Library Version 1, we have:
FuncA(Arg1, Arg2, Arg3) as Boolean
Library Version 2 we have the same but with 4 arguments:
FuncA(Arg1, Arg2, Arg3, Arg4) as Boolean

If I use always 4 arguments I will not be able to compile in using Library Version 1.

Does anyone know if it is possible to have such flexibility in VB?

William Vidal.
May 9 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 8,434
I don't have much experience in this area. But I wonder, if there is some way you can interrogate the libary for its version, then perhaps you can follow different paths in your VB code. Something along the lines of...
Expand|Select|Wrap|Line Numbers
  1. If Library-Version = 1 Then
  2.   Value = FuncA(Arg1, Arg2, Arg3)
  3. ElseIf Lubrary-Version = 2 Then
  4.   Value = FuncA(Arg1, Arg2, Arg3, Arg4)
May 9 '07 #2

P: 3
Thanks Killer,

But in this way the code will not compile once it will expect to match exactly the number of arguments the library has. In this way you will receive during compilation: "Argument not Optional" in case you are in version 2 of the library (which asks for 4 arguments)
Actually this is a mistake from the library designers, they should have done the library with this new 4th argument as Optional.

But I found a way using Conditional Compilation. Follows the code:

Expand|Select|Wrap|Line Numbers
  1. #Const LibVer1 = "Version 1.0"
  2. #Const LibVer2 = "Version 2.0"
  4. Function AuxFuncA(Arg1, Arg2, Arg3, Optional Arg4) as Boolean
  5. Dim CurrentLibVersion as string
  6. 'The Function or method to find out the library version
  7. CurrentLibVersion = GetLibVersion() 
  9. 'Now the Compiler Switches
  10. #If CurrentLibVersion = LibVer1 Then
  11.       AuxFuncA = FuncA(Arg1, Arg2, Arg3)
  12. #ElseIf CurrentLibVersion = LibVer2 Then
  13.       AuxFuncA = FuncA(Arg1, Arg2, Arg3, Arg4)
  14. #End If
  16. End Function
Then just replace in your code all functions FuncA to AuxFuncA.
The good thing is that with conditional compilation VB will not try to compile this region between #If ... #End If before you are really using it.

Thanks anyway,

William Vidal.
May 9 '07 #3

Expert 5K+
P: 8,434
Ah! Good idea.

I didn't realise you had the luxury of running different versions of your program on the different machines.

And yes, I suppose the number of parameters could be a problem at compile time. I wonder whether you could get around it by defining two aliases for the function, with different parameters. I have seen that done to make things safer by defining an alias which constrains certain parameters to certain data types, but never changing the number of parameters (for obvious reasons).
May 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.