Overloading functions

K

Kejpa

Hi,
I have a function Sum as....
Function Sum(ByVal ParamArray Values() As Double) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function

This function is general enough I think, but troubles arise when I try to
calculate the sum of an array of singles. Do I really have to make an
overloaded function for single, long, integer, short, byte and whatnot?
Or is there a better approach?

TIA
/Kejpa
 
C

Cor Ligthert

Kejpa,

I will not say that it is a better approach, because I never see a reason
for this kind of functions in a well made program.

You can also make an overloaded function with every array in the value type
you want to use. For what I see again no reason in this type of functions.

Writing the code to do what you do in this function is almost the same as
calling the function and shows directly what happens.

However beneath when you want to do it the in my opinion less worse.

\\\
Public Class Hello
Public Shared Sub main()
Dim param(3) As Object
Dim a As Integer = 1
Dim b As Double = 2
Dim c As Short = 3
Dim d As Single = 4
param(0) = a
param(1) = b
param(2) = c
param(3) = d
Dim f As Double = CDbl(Sum(param))
Dim g As Integer = CInt(Sum(param))
Dim h As Single = CSng(Sum(param))
End Sub
Public Shared Function Sum(ByVal ParamArray _
Values() As Object) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function
End Class
///

However I hope this helps?

Cor
 
K

Kejpa

Cor,
I can see your point in thinking it's a useless function, but I'd like it in
order to have a "complete" set of statistical functions (including average,
square sum, varians etc etc)

Your code looked great and I anticipated it to work, however it didn't. An
InvalidCastException occurs Conversion from Single() to Double is not valid.
Somehow it seems like the whole ParamArray tries to convert to a Double
instead of each element.
Did you try your code?

Thanks
/Kejpa
 
C

Cor Ligthert

Kejpa,

This strange constructions I mostly test.

f = 10.0
g = 10
h = 10.0

Do I answer with this your question?

:)

Are you sure you changed that passing in the function as well from values()
as Double to values() as Object?

Cor
 
K

Kejpa

I found it!
I was passing an array of singles.
I need to pass arrays of singles, integers, longs as well as doubles since I
have a number of functions that return different types. Guess I will have to
resort to overloading after all :(

/Kejpa
 
C

Cor Ligthert

Kejpa,

Did you look at my sample where I have changed that in objects?

Cor
 
C

Cor Ligthert

Kepja

If you can use my sample is as well important if the values you want to send
and want to receive are inside the range of the double. Therefore if there
are longs involved, than you can in my opinion surely not do it as in my
sample.

And than you have to create an overloading function, however in my idea you
will fast see that it is probably an overdone function.

You have to set all sending values first to the value you want to receive
back, before you can use that function.

However it is your choise of course.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Kejpa,
Function Sum(ByVal ParamArray Values() As Double) As Double
The ParamArray keyword simply says that you can pass a variable list of
values instead of passing an array.

Dim s1 As Double = Sum(1, 2, 3, 4, 5, 6)
Dim s2 As Double = Sum(10, 9, 8, 7, 6)

Dim a1() As Double = { 1, 2, 3, 4, 5, 6 }
s1 = Sum(a1)
Dim a2() As Double = { 10, 9, 8, 7, 6 }
s2 = Sum(a2)
Do I really have to make an
overloaded function for single, long, integer, short, byte and whatnot?
Yes you need to overload, as an array of Single is not the same type as an
array of Double, nor an array of Object. And there is no built-in conversion
from an array of Single to an array of Double. There is an built-in implicit
conversion from Single to Double. So when you list the values you can pass
any type & it will implicitly be converted to Double (if the implicit
conversion is available).

Dim s1 As Double = Sum(1, 2, 3, 4, 5, 6)
Dim s3 As Double = Sum(1.0F, 2.0F, 3.0F)
Dim s3 As Double = Sum(1D, 2D, 3D)
Dim s3 As Double = Sum(1L, 2L, 3L)

Hint try the same function without the ParamArray keyword.

Function Sum(ByVal values() As Double) As Double
End Function

Dim a4() As Single
Dim s4 As Double = Sum(a4)

What error do you get, do you understand why. When using ParamArray VB is
attempting to do an implicit conversion to Double rather then implicitly
convert the entire array.



You could simply pass an Array, however you then loose the ParamArray
feature.

Public Function Sum(ByVal values As Array) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function

VB.NET 2003 will implicitly convert each element to Double to perform the
For Each...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top