locked
Overload Resolution, did i miss something? RRS feed

  • Question

  • Hello all, so i'm coding in asp with VB and I noticed something weird,

    Ok so simply put i construct a CUser from an Id in DB

    array.Add(New CUser(UserSelectFullDB(DR.GetValue(0))))
    Yet this line gimme this Error :
    Error 1 Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(pId As Integer)': Argument matching parameter 'pId' narrows from 'Object' to 'Integer'.
    'Public Sub New(pNt As String, [pBottin As Boolean = True])': Argument matching parameter 'pNt' narrows from 'Object' to 'String'.
    'Public Sub New(pUser As CUser)': Argument matching parameter 'pUser' narrows from 'Object' to 'CUser'. C:\Inetpub\wwwroot\MF\App_Code\CUser.vb 164 19 C:\...\MF\

    Ok my question now, why it doesn't recognize my CUser Object returned by method UserSelectFullDB, in result i'm forced to cast like this...

    array.Add(New CUser(CType(UserSelectFullDB(DR.GetValue(0)), CUser)))



    Here are the constructor
    *******************************************
    'Recopy constructor
    Public Sub New(ByVal pUser As CUser)
    Initialize()
    Copy(pUser)
    End Sub
    'Construct from Nt code the user
    Public Sub New(ByVal pNt As String, Optional ByVal pBottin As Boolean = True)
    Initialize()
    If (pBottin = True) Then
    Copy(UserSelectBottinDB(pNt))
    Else
    Copy(UserSelectFullDB(pNt))
    End If
    End Sub
    'Construct from Nt code the user
    Public Sub New(ByVal pId As Integer)
    Initialize()
    Copy(UserSelectFullDB(pId))
    End Sub
    *******************************************
    The function that return a CUser
    Public Shared Function UserSelectFullDB(ByVal pId As Integer) As CUser
    Dim User As New CUser()
    .......................................ect
    Return User
    End Function
    *******************************************

    Thx in Advance



    Wednesday, June 20, 2007 8:20 PM

Answers

  • Imo, it has to do with the fact that it is VB. =D

    Bah I don't have that much time investigate it, was just curious if this was a known issue of false positive or compiler issue.

    Anyway, thx for the help.

    Thursday, June 21, 2007 5:04 PM
  • In some cases when you pass an object to an overloaded method, it can cause the call to become a late bound call - is UserSelectFullDB overloaded (with no object overload), and is DR.GetValue(0)'s return type object? If so, the call to UserSelectFullDB will be late bound, and the return value considered object at compilation time.


     

    Thursday, June 21, 2007 7:16 PM
    Moderator
  • Thanks Alex.  I knew there was a scenario that could result in the loss of type, but I didn't remember excatly what it was.
    Thursday, June 21, 2007 11:59 PM
    Moderator
  • Hi,

     

    Based on your post,  I can not understand  why this ocurrs. If you are convenient, maybe you can post some  relevant code . This will let us know what is wrong.  From your code posted , we're not seeing which leads the CUser to be passed as an Object .

     

    I notice that it is a web application. If it is an Asp project , You can also post this problem to site. You will obtain the more help.

      http://forums.asp.net/

     

    Riquel

     

     

     

    Friday, June 22, 2007 6:01 AM
    Moderator

All replies

  • Since none of the constructors takes an Object explicitly, and a String, an Integer, and a CUser can each be stored in an Object, the compiler doesn't know which constructor you're trying to call when you pass an Object as the parameter.

     

    The call to: UserSelectFullDB(DR.GetValue(0))

    is returning an Object

     

    So you need to cast that object into the appropriate type based on the specific constructor you are tyring to use:

     

    To use the String parameter constructor:

         array.Add(New CUser(UserSelectFullDB(DR.GetValue(0)).ToString))

    To use the Integer parameter constructor:

         array.Add(New CUser(CInt(UserSelectFullDB(DR.GetValue(0)))))

    To use the Cuser parameter contstructor:

         array.Add(New CUser(CType(UserSelectFullDB(DR.GetValue(0)), CUser)))

    Which one you use depends on the actual value returned by UserSelectFullDB(DR.GetValue(0)))

    Does that make sense?

    Wednesday, June 20, 2007 8:46 PM
    Moderator
  • Umm so this

    The function that return a CUser
    Public Shared Function UserSelectFullDB(ByVal pId As Integer) As CUser
    Dim User As New CUser()
    .......................................ect
    Return User
    End Function

    return an Object?

    I already add the solution of casting, what i'm wondering is why in hell I have to do it when my function(
    UserSelectFullDB) return a CUser, not an object, at least  that's what i can read from my code.
    Thursday, June 21, 2007 11:47 AM
  • You're right that it seems odd you should have to do this, however, that's what the error states and as you said, the type cast fixed it.

     

    I just noticed from the original error msg that this is a web application.  Since all you posted was psuedo code it's hard to say if there some other code execution we're not seeing that is causing the CUser to be passed around as an Object genericly.  I have to assume from the way its called that the shared function is declared in the same class that is experiencing the exception.  What class is this and where is it in relation to the CUser class?  You'll probably need to post a more complete code example to find out why this is happening.  And it may be an ASP design issue that needs to be investigated at forums.asp.net.  These forums aren't for web application issues...

    Thursday, June 21, 2007 4:12 PM
    Moderator
  • Imo, it has to do with the fact that it is VB. =D

    Bah I don't have that much time investigate it, was just curious if this was a known issue of false positive or compiler issue.

    Anyway, thx for the help.

    Thursday, June 21, 2007 5:04 PM
  • In some cases when you pass an object to an overloaded method, it can cause the call to become a late bound call - is UserSelectFullDB overloaded (with no object overload), and is DR.GetValue(0)'s return type object? If so, the call to UserSelectFullDB will be late bound, and the return value considered object at compilation time.


     

    Thursday, June 21, 2007 7:16 PM
    Moderator
  • Thanks Alex.  I knew there was a scenario that could result in the loss of type, but I didn't remember excatly what it was.
    Thursday, June 21, 2007 11:59 PM
    Moderator
  • Hi,

     

    Based on your post,  I can not understand  why this ocurrs. If you are convenient, maybe you can post some  relevant code . This will let us know what is wrong.  From your code posted , we're not seeing which leads the CUser to be passed as an Object .

     

    I notice that it is a web application. If it is an Asp project , You can also post this problem to site. You will obtain the more help.

      http://forums.asp.net/

     

    Riquel

     

     

     

    Friday, June 22, 2007 6:01 AM
    Moderator