Call COM object method with optional parameters.

  • Thread starter Thread starter Yong Jiang
  • Start date Start date
Y

Yong Jiang

I am trying to convert some vb code calling COM oboject
methods to C#. Example shown as follows

Dim Stream As New ADODB.Stream
Call Stream.Open()

Stream.Open has three optional parameters. I am trying to
convert it to C# as shown as follows

ADODB.Stream Stream = null;
Stream = new ADODB.Stream();
Stream.Open();

Somehow, C# does not like leaving paramters blank. I don't
know how to get around of it. Any help will be
appreciated.
 
Yong,

C# does not support optional parameters, so you are going to have to
pass in a value of Missing for all of the parameters that you do not want to
pass in. In order to do that, you will want to do the following:

// Get a missing value.
object pobjMissing = System.Reflection.Missing.Value;

// Make the call.
ADODB.Stream pobjStream = new ADODB.Stream();
pobjStream.Open(pobjMissing, pobjMissing, pobjMissing, pobjMissing,
pobjMissing);

Hope this helps.
 
I appreciate your help.

Now I got the following error messages.

Argument '2': convert from 'object'
to 'ADODB.ConnectModeEnum'
Argument '3': convert from 'object'
to 'ADODB.StreamOpenOptionEnum
Argumetn '4': connot convert from 'object' to 'string'
Argumetn '5': connot convert from 'object' to 'string'

After I changed it to the following to make complier happy

Stream.Open(objMissing, (ADODB.ConnectModeEnum)
objMissing, (ADODB.StreamOpenOptionsEnum) objMissing,
(string) objMissing, (string) objMissing);

I got runtime errors: Specified cast is not valid.

Once again. Thank you for the help.
-----Original Message-----
Yong,

C# does not support optional parameters, so you are going to have to
pass in a value of Missing for all of the parameters that you do not want to
pass in. In order to do that, you will want to do the following:

// Get a missing value.
object pobjMissing = System.Reflection.Missing.Value;

// Make the call.
ADODB.Stream pobjStream = new ADODB.Stream();
pobjStream.Open(pobjMissing, pobjMissing, pobjMissing, pobjMissing,
pobjMissing);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Yong Jiang said:
I am trying to convert some vb code calling COM oboject
methods to C#. Example shown as follows

Dim Stream As New ADODB.Stream
Call Stream.Open()

Stream.Open has three optional parameters. I am trying to
convert it to C# as shown as follows

ADODB.Stream Stream = null;
Stream = new ADODB.Stream();
Stream.Open();

Somehow, C# does not like leaving paramters blank. I don't
know how to get around of it. Any help will be
appreciated.


.
 
These parameter are optional params but not variants. You will need to pass in an empty string (string.Empty i think) for params 4
and 5 and use the correct enum for 2 and 3. Only param 1 is a variant.

--
Michael Culley


Yong Jiang said:
I appreciate your help.

Now I got the following error messages.

Argument '2': convert from 'object'
to 'ADODB.ConnectModeEnum'
Argument '3': convert from 'object'
to 'ADODB.StreamOpenOptionEnum
Argumetn '4': connot convert from 'object' to 'string'
Argumetn '5': connot convert from 'object' to 'string'

After I changed it to the following to make complier happy

Stream.Open(objMissing, (ADODB.ConnectModeEnum)
objMissing, (ADODB.StreamOpenOptionsEnum) objMissing,
(string) objMissing, (string) objMissing);

I got runtime errors: Specified cast is not valid.

Once again. Thank you for the help.
-----Original Message-----
Yong,

C# does not support optional parameters, so you are going to have to
pass in a value of Missing for all of the parameters that you do not want to
pass in. In order to do that, you will want to do the following:

// Get a missing value.
object pobjMissing = System.Reflection.Missing.Value;

// Make the call.
ADODB.Stream pobjStream = new ADODB.Stream();
pobjStream.Open(pobjMissing, pobjMissing, pobjMissing, pobjMissing,
pobjMissing);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Yong Jiang said:
I am trying to convert some vb code calling COM oboject
methods to C#. Example shown as follows

Dim Stream As New ADODB.Stream
Call Stream.Open()

Stream.Open has three optional parameters. I am trying to
convert it to C# as shown as follows

ADODB.Stream Stream = null;
Stream = new ADODB.Stream();
Stream.Open();

Somehow, C# does not like leaving paramters blank. I don't
know how to get around of it. Any help will be
appreciated.


.
 
After I used the following statement

Stream.Open(objMissing,
ADODB.ConnectModeEnum.adModeUnknown,
ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,
string.Empty, string.Empty)

it seems to work fine. I can not use empty string for the
second parameter and third parameter.

Thank you for the help. It is very helpful. Have a very
nice thanks giving.

Yong
-----Original Message-----
These parameter are optional params but not variants. You
will need to pass in an empty string (string.Empty i
think) for params 4
and 5 and use the correct enum for 2 and 3. Only param 1 is a variant.

--
Michael Culley


I appreciate your help.

Now I got the following error messages.

Argument '2': convert from 'object'
to 'ADODB.ConnectModeEnum'
Argument '3': convert from 'object'
to 'ADODB.StreamOpenOptionEnum
Argumetn '4': connot convert from 'object' to 'string'
Argumetn '5': connot convert from 'object' to 'string'

After I changed it to the following to make complier happy

Stream.Open(objMissing, (ADODB.ConnectModeEnum)
objMissing, (ADODB.StreamOpenOptionsEnum) objMissing,
(string) objMissing, (string) objMissing);

I got runtime errors: Specified cast is not valid.

Once again. Thank you for the help.
-----Original Message-----
Yong,

C# does not support optional parameters, so you are going to have to
pass in a value of Missing for all of the parameters
that
you do not want to
pass in. In order to do that, you will want to do the following:

// Get a missing value.
object pobjMissing = System.Reflection.Missing.Value;

// Make the call.
ADODB.Stream pobjStream = new ADODB.Stream();
pobjStream.Open(pobjMissing, pobjMissing, pobjMissing, pobjMissing,
pobjMissing);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to convert some vb code calling COM oboject
methods to C#. Example shown as follows

Dim Stream As New ADODB.Stream
Call Stream.Open()

Stream.Open has three optional parameters. I am
trying
to
convert it to C# as shown as follows

ADODB.Stream Stream = null;
Stream = new ADODB.Stream();
Stream.Open();

Somehow, C# does not like leaving paramters blank. I don't
know how to get around of it. Any help will be
appreciated.



.


.
 
Back
Top