|
-
Oct 15th, 2001, 04:55 AM
#1
Thread Starter
Retired VBF Adm1nistrator
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:
Function doSomething(param1 As Type1, Optional param2 As Type2)
'do something
End Function
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 15th, 2001, 05:50 AM
#2
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.
.
-
Oct 15th, 2001, 11:05 AM
#3
Dazed Member
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.
-
Oct 15th, 2001, 11:11 AM
#4
Thread Starter
Retired VBF Adm1nistrator
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]
-
Oct 15th, 2001, 11:45 AM
#5
Dazed Member
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;
}
}
-
Oct 16th, 2001, 02:10 AM
#6
Thread Starter
Retired VBF Adm1nistrator
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]
-
Oct 16th, 2001, 02:40 AM
#7
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.
.
-
Oct 16th, 2001, 02:45 AM
#8
Thread Starter
Retired VBF Adm1nistrator
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]
-
Oct 16th, 2001, 03:15 AM
#9
Well ...
Try using the return data type as Object, perhaps that might help.
.
-
Oct 16th, 2001, 03:20 AM
#10
Thread Starter
Retired VBF Adm1nistrator
pushing OO theory here, lets hope it doesnt push back
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Oct 16th, 2001, 05:20 AM
#11
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.
.
-
Oct 16th, 2001, 12:15 PM
#12
Dazed Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|