Stuart McLachlan
stuart at lexacorp.com.pg
Thu Sep 16 18:13:33 CDT 2010
Explicit conversion of datatypes: Function SumNumbers(Number1 As Byte, Number2 As Byte) As Integer SumNumbers = CInt(Number1) + CInt(Number2) End Function -- Stuart On 17 Sep 2010 at 0:47, Asger Blond wrote: > Hi list, > Just noticed a VB issue I haven't encountered before. > Say I want a function to receive two input parameters of type Byte and > return a value of type Integer. I create a function like this: > > Function SumNumbers(Number1 As Byte, Number2 As Byte) As Integer > SumNumbers=Number1 + Number2 > End Function > > When testing the function I get this: > ?SumNumbers(200,55) -> 255 (OK) > ?SumNumbers(255,1) -> Run-time error 6 Overflow (??!!) > > The last error isn't expected since the return type is declared as > Integer. I can prevent this behaviour by altering the function like > this: Function SumNumbers(Number1 As Integer, Number2 As Byte) As > Integer > SumNumbers=Number1 + Number2 > End Function > > In other words: I have to make room for the return type by declaring a > sufficient large type for one or more of the input parameters. This > certainly looks like a bug to me. And the "solution" of course doesn't > apply if I actually want to restrict the input parameters to Byte and > make room for a return value exceeding Byte. The same issue goes for > other types, e.g. Integer as input type and Long as return type. Any > solution? Asger > > > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com >