Wish Another plea for optional parameters

NeoTechni

Well-Known Member
Licensed User
Longtime User
Since my program is so large, it's getting increasingly difficult to add parameters to functions, cause I don't want to have to go through all the existing code to add default parameter values.

I've been using messy/bad coding habits to get around this, by using a single parameter for multiple things by making use of it's invalid values. ie: If it's always a positive value, I'll give it a negative one to act as a boolean value, then abs() the value. Thus making 1 parameter serve as 2. (This is made possible by your comments-as-documentation feature, so thank you for that. I wish VB6 had that)

Today I made this bit of code to get around the lack of optional parameters:

B4X:
'If WindowTitle contains: [decimal], [integer], [none], [phone] or [text] the input type will be set to it and that text removed from the WindowTitle
Sub Ask(WindowTitle As String, Body As String, Default As String) As String
   Dim Dialog As InputDialog, Value As Int
   Dialog.Input = Default
   WindowTitle = InputType(Dialog, WindowTitle, "decimal", Dialog.INPUT_TYPE_DECIMAL_NUMBERS)
   WindowTitle = InputType(Dialog, WindowTitle, "integer",  Dialog.INPUT_TYPE_NUMBERS)
   WindowTitle = InputType(Dialog, WindowTitle, "none",     Dialog.INPUT_TYPE_NONE)
   WindowTitle = InputType(Dialog, WindowTitle, "phone",    Dialog.INPUT_TYPE_PHONE)
   WindowTitle = InputType(Dialog, WindowTitle, "text",       Dialog.INPUT_TYPE_TEXT)   
   Value = Dialog.Show(Body, WindowTitle, "OK", "CANCEL", "", Null)
   If Value = DialogResponse.POSITIVE Then Return Dialog.Input.Trim
End Sub
Sub InputType(Dialog As InputDialog, WindowTitle As String, Key As String, Value As Int) As String
   If WindowTitle.Contains("[" & Key & "]") Then
     Dialog.InputType = Value
     WindowTitle = WindowTitle.replace("[" & Key & "]", "")
   End If
   Return WindowTitle
End Sub

Whereas with optional parameters, I could just do:
B4X:
'InputType: 0=decimal, 1=integer, 2=none, 3=phone 4=text
Sub Ask(WindowTitle As String, Body As String, Default As String, InputType as int = -1) As String
   Dim Dialog As InputDialog, Value As Int
   Dialog.Input = Default
   select case InputType
       case 0: Dialog.InputType = Dialog.INPUT_TYPE_DECIMAL_NUMBERS
       case 1: Dialog.InputType = Dialog.INPUT_TYPE_NUMBERS
       case 2: Dialog.InputType = Dialog.INPUT_TYPE_NONE
       case 3: Dialog.InputType = Dialog.INPUT_TYPE_PHONE
       case 4: Dialog.InputType = Dialog.INPUT_TYPE_TEXT
   end select 
   Value = Dialog.Show(Body, WindowTitle, "OK", "CANCEL", "", Null)
   If Value = DialogResponse.POSITIVE Then Return Dialog.Input.Trim
End Sub

I'm not sure how your code parser works, but it should be doable by:
-parsing the parameter list of the function declaration to get the optional parameter's default values
-when a missing/empty parameter is detected in the code that calls the function, insert the default value
That way you don't have to support optional parameters however Android/Java does it, you'd be making your own method which would also let you have any parameters optional, not just the last ones
 
Top