Optional parameters?

  • Thread starter Thread starter Lews
  • Start date Start date
L

Lews

Guest
Is there really no way to skip writing optional parameters in C#. In VB you can choose what parameter to set by writing for example (PrintToFile:=True). However in C# you cant do this but this makes things really hard, for example the method below that prints a word document. I want to set 4 or 5 of the 18 parameters but now I have to find the default value for each of them and set it? That will take ages, especially since Im doing alot of these kind of operations.

Please tell me that there is some better way to solve this...

PrintOut ( System.Object Background , System.Object Append , System.Object Range , System.Object OutputFileName , System.Object From , System.Object To , System.Object Item , System.Object Copies , System.Object Pages , System.Object PageType , System.Object PrintToFile , System.Object Collate , System.Object ActivePrinterMacGX , System.Object ManualDuplexPrint , System.Object PrintZoomColumn , System.Object PrintZoomRow , System.Object PrintZoomPaperWidth , System.Object PrintZoomPaperHeight )
 
Make a VB assembly that calls the method you want, and call the method you write in the vb assembly from C#.

C# does not support optional parameters.
 
Be careful with default values for parameters in VB.NET, due to the fact they are not supported by the CLR the values are compiled into the calling assembly.
If you issue a later version of the VB.NET dll with different default values existing code compiled against the original version will continue to use the old defaults.
 
To be honest there should be no need to write optional paremeters. Just use an overloaded method. Solves the whole problem. :)
 
Argh, can everyone stop saying that, because its not true... The problem lies in identifying the default values of every parameter in the operation. Once I know what to send in all the parameters Im not interested in, the problem is solved, then I have no need to make an overloaded operation because that would only make the code look better, and at the moment that is not my problem.
 
Anyways, I found out that there is objects like "oMissing" that you can send in the empty spots :).
 
Can you paste the method youre doing optional paremeters for? Id be more then happy to show you how it can be done with Overloading. And if for some strange odd reason its not possible, then youve just proven a serious advantage that VB.NET has over C#. :)
 
They are all optional I think... The problem is easiest solved by creating an object called:

object oMissing = System.Reflection.Missing.Value;

and then you just put it in the parameters you dont want to submit... well "ref oMissing" in this case...
 
Why not create a structure or class with the parameters as member variables (or properties) and set them to defaults in the sub New(). Then pass an instance of this class to the method?

i.e.

C#:
class test {
System.Object Background ,
System.Object Append ,
System.Object Range ,
System.Object OutputFileName ,
System.Object From ,
System.Object To ,
System.Object Item ,
System.Object Copies ,
System.Object Pages ,
System.Object PageType ,
System.Object PrintToFile ,
System.Object Collate ,
System.Object ActivePrinterMacGX ,
System.Object ManualDuplexPrint ,
System.Object PrintZoomColumn ,
System.Object PrintZoomRow ,
System.Object PrintZoomPaperWidth ,
System.Object PrintZoomPaperHeight,
}

public void PrintOut (test info) {}

also out of curiosity is there any reason why all the parameters are declared object?
 
Last edited by a moderator:
Well, again I will point out that the problem is not making a wrapping method or class that makes the executing easier. The problem is that the PrintOut method comes with the Word 9 Library. This means that Microsoft wrote the method and I have no access to whats in it. And to answer the object question its probably just a standart THEY use.

The problem I had was finding out what the default value was for each of the parameters in the microsoft methods. Since I only had access to the methods interface I couldnt retrieve those values.

However as I said before I found out that in the microsoft case you solve this by using the "oMissing" object.



:)
 
Back
Top