Results 1 to 12 of 12

Thread: Optional Parameters

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Optional Parameters

    Is it possible to allow a parameter to be optionally passed to a method ? I'm currently passing a -1 if I want to ignore the parameter, but I'd prefer if I could pass nothing.

    eg.

    VB Code:
    1. Function doSomething(param1 As Type1, Optional param2 As Type2)
    2.     'do something
    3. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I don't think you can pass any parameters optionally.

    There are two solutions. One is what you have already followed, to pass a dummy value for a parameter so that the method knows that parameter has not been passed.

    The other way is to overload the method, so that one form accepts a single argument and assumes a default value for the other argument, and the other form accepts both arguments. For e.g. in a class called Rectangle, if I have a setSize method which accepts two arguments width and height, and I want to implement it so that if I don't pass the height argument, it should have a default height of 10, I would write it as follows:

    Code:
    class Rectangle
    {  int w, h;
        public void setSize(int width)
        {   w = width;
             h = 10;
        }
        public void setSize(int width, int height)
        {   w = width;
             h = height;
        }
    }
    Also note that you cannot change the sequence of the arguments being passed.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Java does not support optional parameters as Visual basic does. You can declare a method parameters as final but thats about it.
    Method overloading is a good route to go as HoneyBee pointed out.

  4. #4

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I hadnt studied method overloading yet.... I suppose I know what its about now though.

    Thanks for that, dead handy.

    Cheers,
    Jamie.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    It's almost like if you want to overload a class constructor.
    This was you have a chioce of what parameters you want to pass into it. "this" is often used in a constructor to call a diffrent constructor of the same class. The "this" is commonly refered to as a constructor call statement. Super is often used to call a super
    class constructor from within a subclasses constructor. If the supper class fails to have a constructor with no arguements then an error will be thrown.

    public class X{
    double x, y, width, height

    public X(){
    this(1,1)
    }

    public X(double x, double y){
    this(x,y,10,10);
    }

    public X(double x, double y, double width, double height){
    super(); // note the implicit super being inserted.
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    }
    }

  6. #6

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hmmm interesting.
    On a similar note, then I take it if you want to allow methods to return different datatypes, then you would just copy and paste the method a number of types, and then change the returned datatype ?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    With C++ it would be so. I am not so sure about Java. Our teacher had once said that to overload a method in Java, you had to specify different number of arguments, so if you had a method as follows:

    public void Overload(int x)
    {
    //blah blah of course
    }

    public void Overload(String S)
    {
    //more blah blah
    }

    this would not be overloading, as both methods accept just one parameter.

    I guess the above statement is wrong, as I just finished reading an O'Reilly book on networking with Java which has several examples of overloaded methods accepting the same number of arguments.

    So, I would bet that by changing the return data type you can overload a method as well.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Pain in the ass that you cant return a Variant or do a more intelligent return. Reason being that I've implimented Split() and ReDim() and a few other functions in VB.

    But say for example ReDim(), I have to specify the data type to use. So each project I do I normally have 2 or 3 versions of ReDim.
    At present im calling them things like :

    strReDim
    intReDim
    sngReDim

    I suppose now that I can overload them with datatypes it will make the code a little better, but its still a pain in the ass
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Try using the return data type as Object, perhaps that might help.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  10. #10

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    pushing OO theory here, lets hope it doesnt push back
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  11. #11
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by plenderj
    pushing OO theory here, lets hope it doesnt push back
    If you mean it should not backfire, I guess it won't. It's similar to handling exceptions by way of trapping a single exception object Exception instead of going for individual exceptions. Or compare it to using the Control object in VB to use for TextBox, CheckBox, List or any other control.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  12. #12
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I guess the above statement is wrong, as I just finished reading an O'Reilly book on networking with Java which has several examples of overloaded methods accepting the same number of arguments.

    So, I would bet that by changing the return data type you can overload a method as well.
    Changing the return data type will not be enough to overload
    a method in Java since the return type is not considered part of
    the method. Why this is so im not sure. The return type should
    be considered but i think Java does not permit this becuase then you could just keep the same signature "arguements" and just change the return type. To overload a method the arguements
    just have to differ in type, order, or number.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width