Guest Lews Posted November 26, 2002 Posted November 26, 2002 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 can't 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 I'm 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 ) Quote
*Gurus* divil Posted November 26, 2002 *Gurus* Posted November 26, 2002 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Administrators PlausiblyDamp Posted November 26, 2002 Administrators Posted November 26, 2002 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
wyrd Posted November 26, 2002 Posted November 26, 2002 To be honest there should be no need to write optional paremeters. Just use an overloaded method. Solves the whole problem. :) Quote Gamer extraordinaire. Programmer wannabe.
Guest Lews Posted November 27, 2002 Posted November 27, 2002 Argh, can everyone stop saying that, because it's 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 I'm 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. Quote
Guest Lews Posted November 27, 2002 Posted November 27, 2002 Anyways, I found out that there is objects like "oMissing" that you can send in the empty spots :). Quote
wyrd Posted November 27, 2002 Posted November 27, 2002 Can you paste the method you're doing optional paremeters for? I'd be more then happy to show you how it can be done with Overloading. And if for some strange odd reason it's not possible, then you've just proven a serious advantage that VB.NET has over C#. :) Quote Gamer extraordinaire. Programmer wannabe.
Guest Lews Posted November 27, 2002 Posted November 27, 2002 You find the method in the first post... Quote
wyrd Posted November 27, 2002 Posted November 27, 2002 Ah, well then. :) Which parameters are optional. Or are they all optional? Quote Gamer extraordinaire. Programmer wannabe.
Guest Lews Posted November 28, 2002 Posted November 28, 2002 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 don't want to submit... well "ref oMissing" in this case... Quote
Administrators PlausiblyDamp Posted November 29, 2002 Administrators Posted November 29, 2002 (edited) 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. 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? Edited December 10, 2002 by Thinker Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Guest Lews Posted November 29, 2002 Posted November 29, 2002 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 what's in it. And to answer the object question it's 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 couldn't retrieve those values. However as I said before I found out that in the microsoft case you solve this by using the "oMissing" object. :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.