Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Default parameters for ActiveX controls.

Is it possible to define default settings for ActiveX methods in VC++6.0? Something like in C++:

void NewItem(int size, int type = 0);
0
sdj
Asked:
sdj
1 Solution
 
Answers2000Commented:
I don't think you can do this.

I don't think you can overload methods with the same name and different parameter lists either.

Presumably this is because COM is supposed to be language independant, and this would not be allowable in some languages.

Why not simply define 2 methods

void NewItem( int size )

and

void NewItem2( int size, int type )


Both functions can use the same code (e.g. NewItem, calls NewItem2 with type = 0, or both calling some underlying internal function to do the work ).
0
 
sdjAuthor Commented:
Not a very user friendly approach. But I here what you are saying.
0
 
Tommy HuiCommented:
You can have optional arguments. However, you need to write the function method yourself. You can use the optional keyword in the IDL file to specify that the argument to the function is optional. The argument type must be either VARIANT or VARIANT*.

However, to implement default arguments, you need to check for VT_EMPTY in that argument and supply your own default.


0
 
sdjAuthor Commented:
Any chance of a sample, or a pointer to where I may find some useful code?
0
 
gvgCommented:
The ODL file would have a line like this.

[id(2)] long increase( long value, [optional] VARIANT incVal )

Then you would have a function like this

long increase( long value, VARIANT incVal )
{
  VARIANT varCon;
 
  // Init a the new variant.
  ::VariantInit(&varCon);

  // Try to convert the incoming variant to I4 ( 4 byte integer )
  if (S_OK = ::VariantChangeType(&varCon, &incVal, 0, VT_I4 );
    return value + varCon.lVal;
  else
    return value + 1;
}

If you use VB to call this function you only have to give one paramter and VB will take care of the rest.  If how ever you use vc++ you must pass both arguments and you must make sure that the vvariant you pass is VT_EMPTY if you would like to use default behavior.  All you need to do to create an empty variant is

VARIANT emptyVar;
VariantInit( &emptyVar );

VariantInit sets the variant type to empty.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now