Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
Hi All,

I have a bit of an odd request, I'd like to overload a function. Yes, yes, you all will think but that is easy. But I don't want to overload it in the conventional way of feeding two values to the function and the overloading is done by having the values as double or integer or whatever the case might be...

I need the following:

VB
public function circle (byval diameter as double, byval radius as double, byval area as double, byval cirumference as double) as double

'Depending on which byval I fill in, and which one I indicate with say a "0", the 0 unknown is returned

end function
Posted
Updated 6-Feb-12 20:28pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 2:28am    
Corrected tags. VB.NET is not VB. Tags are important.
--SA
Sergey Alexandrovich Kryukov 7-Feb-12 2:30am    
What do you call "overload". Or, in other words, where is your second function?
--SA
JacquesGrobler 7-Feb-12 2:38am    
Do I have to have more than one function to overload?

And if so, what do I then call what I am trying to do?
Sergey Alexandrovich Kryukov 7-Feb-12 2:44am    
Always. Otherwise if is not called "overloading". Bad term, confusing. We don't have to use it to explain the syntax and semantic.

I explained everything you may need in my answer, please see.
--SA
JacquesGrobler 7-Feb-12 6:32am    
Anybody able to assist me please?

Maybe you don't know what is overloading. No wonder, the term is quite confusing, because nothing is actually "loaded", so what could be "overloaded"? Actually, nothing.

It really means that two or more methods have the same name (in the same class or structure, that is, the same fully-qualified name), but different signatures, and each particular signature could be recognized be the compiler by the call. That said, depending on the particular call parameters in the code of the actual call, such recognition may or may not be possible, and the ambiguity will cause compilation error.

Additionally, you should understand that VB.NET is still a language with strong typing, despite of implicit conversion typical for this language. Please see http://en.wikipedia.org/wiki/Strong_typing[^].

That said: if your idea is a dynamic parameter, it's a wrong idea. If you formal parameter is double, it will always be double, even if the actual parameter is int — it is merely converted to double, so, from the standpoint of the method implementation, it is always double and nothing else.

Actually, your question is not clear and/or incorrectly formulated and/or makes no sense. Not to worry — I hope my explanations are enough for you to sort things out.

—SA
 
Share this answer
 
v2
Comments
JacquesGrobler 7-Feb-12 5:04am    
Thanks for the reply SA,

I understand a bit more about overloading now, and yes, I suppose this is not what I am looking for...

Let me try to put it in another way:

I have four parameters that can be optional in the above function, thus I can fill in only one of these four with a double varialbe, and also obtain any ONE of these four variables.

How can I achieve this?

If my question is still not very clear, please let me know and I'll try to approach it from another angle.

Jacques
Dave Kreskowiak 7-Feb-12 10:51am    
There is nothing that can enforce this for you. You would either have to have four seperate functions, each taking only one parameter, or you would have to include code in the beginning of the function to validate that only one value is non-zero, then branch to the appropriate code that handles the case of which value was supplied.

Personally, I'd use four seperate functions. I have no idea what your mean by "circle". The name doesn't describe what the method does so I can't really say exactly how I'd implement this. All we do know is that you supply a value and then return a value. What you do in between is not known.

Want you want to do is possible, but in my opinion you are making it harder than it needs to be. Instead of making a function, why not make a simple method? Pass your variables in ByRef (By Reference) so you can update them directly:
VB
Public Sub circle(ByRef diameter As Double, ByRef radius As Double, ByRef area As Double, ByRef cirumference As Double)

If diameter <> 0 Then
   'Calculate all parameters other than diameter
End If 

If radius <> 0 Then
   'Calculate all parameters other than radius
End If

'And so on...

End Sub


This way, when call the method, you already have all the variables setup to retrieve the proper values.
VB
Dim diameter as Double = 10
Dim radius as Double = 0
Dim area as Double = 0
Dim circumference as Double = 0
circle(diameter, radius, area, circumference)
'Now you already know which "return" value is for which variable
 
Share this answer
 
Comments
JacquesGrobler 8-Feb-12 0:25am    
Thanks Kschuler,

I'll give this a try.
Something like this is what I guess you want:

public static double Circle(double? diameter = null, double? radius = null, double? area = null, double? circumference = null)
{
    double result = 0.0;

    if (diameter.HasValue)
    {
        // Do Something
    }

    if (radius.HasValue)
    {
        // Do Something
    }

    if (area.HasValue)
    {
        // Do Something
    }

    if (circumference.HasValue)
    {
        // Do Something
    }

    return result;
}


You can then call this with named parameters:

Circle(radius: 10.0);


Sorry it's in C#, but that's what I know.
 
Share this answer
 
Comments
JacquesGrobler 8-Feb-12 0:25am    
Morning Steve,

I'll give this a try as well. I'll first convert it to vb, but then I will try to use it.
SteveAdey 8-Feb-12 15:15pm    
Hi Jacques, did you get a chance to try this out in VB?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900