• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • 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);
1 Solution
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 )


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 ).
sdjAuthor Commented:
Not a very user friendly approach. But I here what you are saying.
Tommy HuiEngineerCommented:
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.

sdjAuthor Commented:
Any chance of a sample, or a pointer to where I may find some useful code?
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.

  // Try to convert the incoming variant to I4 ( 4 byte integer )
  if (S_OK = ::VariantChangeType(&varCon, &incVal, 0, VT_I4 );
    return value + varCon.lVal;
    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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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