Passing optional parameters in C#, out Object

K

Krish

I have a method who signature looks like :

void methodName(Object security, int cookie, object Fields, object
OverrrideFlds,object Overrides, out object resut, object Monitor)


So far I have used Type.Missing if the parameter is optional, In the
signature
above there is "out object result" when I pass Type.Missing Iget

...... has some invalid arguments
Argument 6 cannot convert from object to out object.


a) what is out object ?
b) What do I pass something that will say I donot want to really pass
this paramter .


thanks for any help.
 
D

Daniel Pratt

Hi Krish,

Krish said:
I have a method who signature looks like :

void methodName(Object security, int cookie, object Fields, object
OverrrideFlds,object Overrides, out object resut, object Monitor)


So far I have used Type.Missing if the parameter is optional, In the
signature
above there is "out object result" when I pass Type.Missing Iget

..... has some invalid arguments
Argument 6 cannot convert from object to out object.


a) what is out object ?
b) What do I pass something that will say I donot want to really pass
this paramter .

A few things: First, I would recommend that you do not use Missing.Value
to represent missing parameters. Missing.Value only exists for compatibility
with legacy COM components. To use it you must also use "object" parameters,
which makes you lose all the wonderful type-checking features of the
language. A much better idea is to create different method signatures using
overrides.

Second, there are more restrictions on what you can pass to "out" or
"ref" parameters than normal parameters:

1. You must add the "out" or "ref" modifier to the variable you pass to
the parameter (i.e. pass "out someVariable" instead of just "someVariable").
2. The variable you pass to the parameter must be an "lvalue", meaning
it must be something that can have a value assigned to it. For example, you
cannot assign a value to Type.Missing, therefore it is not an "lvalue".
3. The variable you pass must be the exact same type as the parameter.

Third, it is not possible for a method that has an "out" parameter to
see what value was passed in via the parameter (in fact there is no value
passed in).

Hope that helps.

Regards,
Dan
 

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