Click to See Complete Forum and Search --> : Optional Parameters
plenderj
Oct 15th, 2001, 04:55 AM
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.
Function doSomething(param1 As Type1, Optional param2 As Type2)
'do something
End Function
honeybee
Oct 15th, 2001, 05:50 AM
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:
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.
.
Dillinger4
Oct 15th, 2001, 11:05 AM
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.
plenderj
Oct 15th, 2001, 11:11 AM
I hadnt studied method overloading yet.... I suppose I know what its about now though.
Thanks for that, dead handy.
Cheers,
Jamie.
Dillinger4
Oct 15th, 2001, 11:45 AM
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;
}
}
plenderj
Oct 16th, 2001, 02:10 AM
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 ?
honeybee
Oct 16th, 2001, 02:40 AM
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.
.
plenderj
Oct 16th, 2001, 02:45 AM
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
honeybee
Oct 16th, 2001, 03:15 AM
Try using the return data type as Object, perhaps that might help.
.
plenderj
Oct 16th, 2001, 03:20 AM
pushing OO theory here, lets hope it doesnt push back :)
honeybee
Oct 16th, 2001, 05:20 AM
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.
.
Dillinger4
Oct 16th, 2001, 12:15 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.