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 14 1395
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
"Kejpa" <kS*******@saj.fi>
... 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
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
"Kejpa" <kS*******@saj.fi>
... 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
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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
"Kejpa" <kS*******@saj.fi> .. 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
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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
"Kejpa" <kS*******@saj.fi> .. 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
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
"Kejpa" <kS*******@saj.fi> 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
"Cor Ligthert" <no************@planet.nl> wrote in message news:%2****************@tk2msftngp13.phx.gbl... 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
"Kejpa" <kS*******@saj.fi> .. > 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 > >
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
"Kejpa" <kS*******@saj.fi> 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
"Cor Ligthert" <no************@planet.nl> wrote in message news:%2****************@tk2msftngp13.phx.gbl... 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
"Kejpa" <kS*******@saj.fi> .. > 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 > >
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eb****************@TK2MSFTNGP14.phx.gbl... 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
"Kejpa" <kS*******@saj.fi>
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
"Cor Ligthert" <no************@planet.nl> wrote in message news:%2****************@tk2msftngp13.phx.gbl... 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
"Kejpa" <kS*******@saj.fi> .. > 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 > >
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eb****************@TK2MSFTNGP14.phx.gbl... 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
"Kejpa" <kS*******@saj.fi>
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
"Cor Ligthert" <no************@planet.nl> wrote in message news:%2****************@tk2msftngp13.phx.gbl... 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
"Kejpa" <kS*******@saj.fi> .. > 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 > >
Kejpa,
Did you look at my sample where I have changed that in objects?
Cor
"Kejpa" <kS*******@saj.fi> 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 "Cor Ligthert" <no************@planet.nl> wrote in message news:eb****************@TK2MSFTNGP14.phx.gbl... 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
"Kejpa" <kS*******@saj.fi>
> 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 > > > "Cor Ligthert" <no************@planet.nl> wrote in message > news:%2****************@tk2msftngp13.phx.gbl... >> 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 >> >> "Kejpa" <kS*******@saj.fi> >> .. >> > 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 >> > >> > >> >> > >
Kejpa,
Did you look at my sample where I have changed that in objects?
Cor
"Kejpa" <kS*******@saj.fi> 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 "Cor Ligthert" <no************@planet.nl> wrote in message news:eb****************@TK2MSFTNGP14.phx.gbl... 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
"Kejpa" <kS*******@saj.fi>
> 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 > > > "Cor Ligthert" <no************@planet.nl> wrote in message > news:%2****************@tk2msftngp13.phx.gbl... >> 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 >> >> "Kejpa" <kS*******@saj.fi> >> .. >> > 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 >> > >> > >> >> > >
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
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
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
"Kejpa" <kS*******@saj.fi> wrote in message
news:cp**********@gandalf.alcom.aland.fi... 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
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
"Kejpa" <kS*******@saj.fi> wrote in message
news:cp**********@gandalf.alcom.aland.fi... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Terje Slettebø |
last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been
discussed before, I'd appreciate a pointer to it. I again haven't found it
in a search of the PHP groups.
The PHP...
|
by: Xiangliang Meng |
last post by:
Hi, all.
When reading C++ books, I'm alway confused by those terms "redefining
functions", "overloading functions" and "overriding functions".
Please give me some comments on those terms....
|
by: Susan Baker |
last post by:
Hello everybody,
I'm new to C++ (I have some C background). I've read up on this topic a
few times but it just dosen't seem to be sinking in.
1. Whats the difference between overloading and...
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
|
by: zeus |
last post by:
I know function overloading is not supported in C.
I have a few questions about this:
1. Why? is it from technical reasons? if so, which?
2. why wasn't it introduced to the ANSI?
3. Is there any...
|
by: Doug |
last post by:
Hi
I am 'returning' to the learning of C# after a change in jobs and I remember
that I used to struggle with the defintion, purpose and function of
'overloading'. When I was beginning with C# I...
|
by: Jerry Fleming |
last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), ,...
|
by: Chameleon |
last post by:
What is better if you want upcasting in intermediate classes like below?
Multiple Inheritance and Overloading or simply RTTI?
RTTI wants time but MI and Overloading create big objects
(because of...
|
by: looping |
last post by:
Hi,
for the fun I try operator overloading experiences and I didn't
exactly understand how it works.
Here is my try:
def __pow__(self, value):
return self.__add__(value)
6
|
by: yashwant pinge |
last post by:
#include<iostream>
using namespace std;
class base
{
public:
void display()
{
}
};
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |