rmatthew Posted January 14, 2003 Posted January 14, 2003 Ack - seems I am always in here admiting I don't know something :) In any case is there a way to see if an optional paramater was included in the original call to a function or sub? Thanks Quote
Moderators Robby Posted January 14, 2003 Moderators Posted January 14, 2003 I guess you can check if the value is not the same as the default (value) Quote Visit...Bassic Software
rmatthew Posted January 14, 2003 Author Posted January 14, 2003 Yeah I guess - but seems like their was a way to do this in VB6 that was much cleaner. Quote
*Experts* Volte Posted January 14, 2003 *Experts* Posted January 14, 2003 You shouldn't be using Optional parameters anyway. You should overload the sub. Private Sub SomeFunction(param As String) msgBox param & " was passed!" End Sub Private Sub SomeFunction() MsgBox "Nothing was passed." End Sub Quote
*Experts* Nerseus Posted January 14, 2003 *Experts* Posted January 14, 2003 To add what VolteFace said (thanks, btw, didn't know VB.NET supported overloads): You can mimic your own custom optional by moving all of your "real" code into the function that takes the params. For example: Private Sub SomeFunction() Console.WriteLine("Nothing passed") SomeFunction(String.Empty) End Sub Private Sub SomeFunction(param As String) ' Can you use + in VB.NET or is that C# only? ' You might need to use & Console.WriteLine("Passed: " + param) End Sub This is handy in a lot of cases where you want to use an optional parameter but you don't want to be VB.NET specific. In the example above, the param isn't really optional, but you CAN call it as: SomeFunction() or SomeFunction("Test") -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
rmatthew Posted January 14, 2003 Author Posted January 14, 2003 What is wrong with optional paramaters :) Sub is awfully long to override. Quote
*Experts* Nerseus Posted January 14, 2003 *Experts* Posted January 14, 2003 If I remember right, the compiled code that gets created is basically creating overloads for all of your optional arguments. You get more control with your own overloads and, I think, they're easier to use. You only have to code the overload once and hopefully you don't have TOO many optional params. If you have more than a couple of optionals you may be trying to punch too much bang for the buck into one function OR you probably end up only using 3 or 4 "versions" of the function (meaning, which params are passed and which are left "optional"). And, don't be surprised if VB.NET version 3 cuts out the optionals :) -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* Bucky Posted January 14, 2003 *Experts* Posted January 14, 2003 Nerseus, in VB.NET you can use either + or & to concatenate strings, but it is much better if you use & only for concatenation. Plus, if you're doing a LOT of concatenating, use a StringBuilder class, instead. :) Save the + operator for addition. [edit] Also, relating back to the thread subject... I never use optional parameters; I always overload the methods. It helps organize things, and the IntelliSense is nicer, too. :) [/edit] Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* divil Posted January 14, 2003 *Gurus* Posted January 14, 2003 No, the compiled code doesn't create overloads. The .NET framework itself DOES support optional parameters, and it gets compiled just fine. It is C# itself that doesn't 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
*Experts* Nerseus Posted January 14, 2003 *Experts* Posted January 14, 2003 Ah, works the same as VB6 then. Goooood to know. :) -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
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.