Overload Resolution error

  • Thread starter Thread starter Gang Zhang
  • Start date Start date
G

Gang Zhang

Hi,

I have 2 overloaded functions as below:

myfunc(byval val as Decimal) as string
myfunc(byval val as String) as string

When calling the function with a single like:

dim num1 as single = ...

myfunc(num1)

I get an compiler error saying:
------------------------------------------
error BC30519: Overload resolution failed because no accessible "myfunc" can
be called without a narrowing conversion:



'Public Shared Overloads Function myfunc(val As Decimal) As String':
Argument matching parameter 'val' narrows from 'Double' to 'Decimal'.



'Public Shared Overloads Function myfunc(val As String) As String': Argument
matching parameter 'val' narrows from 'Double' to 'String'.

------------------------------------------



What I don't understand is, should it be widening from Double to Decimal?
Why is the compiler trying to do

narrowing from Double to Decimal?



Thanks!
 
Gang Zhang said:
Hi,

I have 2 overloaded functions as below:

myfunc(byval val as Decimal) as string
myfunc(byval val as String) as string

When calling the function with a single like:

dim num1 as single = ...

myfunc(num1)

I get an compiler error saying:
------------------------------------------
error BC30519: Overload resolution failed because no accessible
"myfunc" can be called without a narrowing conversion:

'Public Shared Overloads Function myfunc(val As Decimal) As
String': Argument matching parameter 'val' narrows from 'Double' to
'Decimal'.

'Public Shared Overloads Function myfunc(val As String) As String':
Argument matching parameter 'val' narrows from 'Double' to
'String'.

------------------------------------------

What I don't understand is, should it be widening from Double to
Decimal? Why is the compiler trying to do

narrowing from Double to Decimal?

Enable Option Strict and you'll get the "real" error message.

Without Option Strict: I get a different message: "...narrwos from 'SINGLE'
to Decimal". Where does your "double" come from? Nevertheless, I don't see
the point: None of the signatures fit. Conversion from Single to Decimal is
a narrowing, not a widening conversion. So, not even implicit conversion
can be done. Therefore the error. Pass CDec(num1) (which might lead to a
runtime error) or Num1.ToString.
 
A single is not a decimal, change one of them to match the other

Tom--
==========================================
= Tom Vande Stouwe MCSD.net, MCAD.net, MCP
= 45Wallstreet.com (www.45wallstreet.com)
= (803)-345-5001
==========================================
= If you are not making any mistakes
..= ..you are not trying hard enough.
==========================================
 
Gang Zhang said:
Thanks for the reply.

I understand a Single type is different from a Decimal type. And
I could have added another overloaded function that takes a Single
type input.

However, the question remains that the VB.NET compiler
should have done the automatical widening of Single(4 bytes) to a
Decimal( 16 bytes)

That's narrowing, not widening.

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB/vbcn7/html/vaconWideNarrowConversi
ons.htm

(see hints in signature)

For example, the biggest positive value for Single is 3.4028235E+38
whereas it is only about 79E+28 for the Decimal data type.
and used the existing function instead of
giving a compiler error on attempting to do narrowing.



--
Armin

- Links might be split into two lines. Concatenate them using notepad.
- Links might require to add a ".nnnn" after the "2003FEB", e.g.
"2003FEB.1033" for localized versions.
- Links starting with "ms-help" are URLs for the document explorer (<F1>).
Paste them in the URL textbox and press enter. Using internal help (menu
tools -> options -> environment -> help), display the "Web" toolbar that
contains the textbox.
 
..Net doesn't convert anything to another datatype automaticly like it would
in VB6, that's why Ctype was added..
 
Thanks all for the help!



Jay B. Harlow said:
Gang,
But Single to Decimal is a narrowing not a widening conversion!

It not based on number of bytes as Single has a wider range then Decimal!
Although Decimal does has more precision. Single to Decimal would loose the
range, hence its not allowed. Decimal to Single would loose the precision,
so its rounded and allowed.

See the conversion section of the Visual Basic .NET Language Specification

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/
vblrfVBSpec10.asp

Specifically Narrowing Conversions:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/
vblrfVBSpec10_4.asp

Very specifically Narrowing Numeric Conversions:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/
vblrfVBSpec10_4_1.asp

Hope this helps
Jay
 
Back
Top