Overload resolution failed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Burton

    #1

    Overload resolution failed

    I have a base (MustInherit/abstact) class that looks like:

    Public MustInherit Class BaseClass
    End Class

    Then I have three derived classes:

    Public Class DerivedA
    Inherits BaseClass
    End Class

    Public Class DerivedB
    Inherits BaseClass
    End Class

    Public Class DerivedC
    Inherits BaseClass
    End Class

    Since each of these classes are handled differently I have three Shared
    Overloaded Sub's

    Public Shared Sub BuildMessage(By Val Product As DerivedA)
    End Sub

    Public Shared Sub BuildMessage(By Val Product As DerivedB)
    End Sub

    Public Shared Sub BuildMessage(By Val Product As DerivedC)
    End Sub

    When I compile I get:

    Overload resolution failed because no accessible 'BuildMessage' can be
    called with these arguments:

    'Public Shared Sub BuildMessage(Pr oduct As DerivedA)':
    Option Strict On disallows implicit conversions from 'BaseClass' to
    'DerivedA'.
    'Public Shared Sub BuildMessage(Pr oduct As DerivedB)':
    Option Strict On disallows implicit conversions from 'BaseClass' to
    'DerivedB'.
    'Public Shared Sub BuildMessage(Pr oduct As DerivedC)':
    Option Strict On disallows implicit conversions from 'BaseClass' to
    'DerivedC'.

    This type of thing is done all the time with C# and C++. Since I am
    relatively new to VB I am not sure how to get around this error. I would
    rather not globally turn 'Option Strict' off, but if that is the only way
    then I guess I must. Suggestions?

    Thank you.

    Kevin

  • Herfried K. Wagner [MVP]

    #2
    Re: Overload resolution failed

    "Kevin Burton" <KevinBurton@di scussions.micro soft.com> schrieb:[color=blue]
    >I have a base (MustInherit/abstact) class that looks like:
    >
    > Public MustInherit Class BaseClass
    > End Class
    >
    > Then I have three derived classes:
    >
    > Public Class DerivedA
    > Inherits BaseClass
    > End Class
    >
    > Public Class DerivedB
    > Inherits BaseClass
    > End Class
    >
    > Public Class DerivedC
    > Inherits BaseClass
    > End Class
    >
    > Since each of these classes are handled differently I have three Shared
    > Overloaded Sub's
    >
    > Public Shared Sub BuildMessage(By Val Product As DerivedA)
    > End Sub
    >
    > Public Shared Sub BuildMessage(By Val Product As DerivedB)
    > End Sub
    >
    > Public Shared Sub BuildMessage(By Val Product As DerivedC)
    > End Sub
    >
    > When I compile I get:
    >
    > Overload resolution failed because no accessible 'BuildMessage' can be
    > called with these arguments:
    >
    > 'Public Shared Sub BuildMessage(Pr oduct As DerivedA)':
    > Option Strict On disallows implicit conversions from 'BaseClass' to
    > 'DerivedA'.
    > 'Public Shared Sub BuildMessage(Pr oduct As DerivedB)':
    > Option Strict On disallows implicit conversions from 'BaseClass' to
    > 'DerivedB'.
    > 'Public Shared Sub BuildMessage(Pr oduct As DerivedC)':
    > Option Strict On disallows implicit conversions from 'BaseClass' to
    > 'DerivedC'.[/color]

    I think you forgot to post the method call:

    \\\
    Dim c As BaseClass = ...
    BuildMessage(c)
    ///

    This will raise the compile-time error you stated above because the type of
    the object referenced by 'c' isn't known at compile-time and maybe there is
    another derived type which is neither 'DerivedA' nor 'DerivedB' nor
    'DerivedC' and thus the call would fail at runtime.

    To solve the problem, cast 'c' to the type of the object or use the derived
    type instead of 'BaseClass' for the variable. Alternatively you could
    remove the overloaded methods and type the parameter 'Product' as
    'BaseClass', check the type of the object passed into the method and throw
    an 'ArgumentExcept ion' if it is none of the predefined derived types.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • RMT

      #3
      Re: Overload resolution failed

      I can't quite understand whats going on here - is BuildMessage a member of
      the base class? If so, declare it overridable and then override it once in
      each derived class.

      Public MustInherit Class BaseClass

      Public MustOverride Sub BuildMessage ()

      End Class

      Public Class DerivedA
      Inherits BaseClass

      Public Overrides Sub BuildMessage ()
      ...
      End Sub

      End Class

      Public Class DerivedB
      Inherits BaseClass

      Public Overrides Sub BuildMessage ()
      ...
      End Sub

      End Class

      Public Class DerivedC
      Inherits BaseClass

      Public Overrides Sub BuildMessage ()
      ...
      End Sub

      End Class

      "Kevin Burton" <KevinBurton@di scussions.micro soft.com> escribió en el
      mensaje news:9E928987-F3E0-4799-BDE1-47E5D43DF4CB@mi crosoft.com...[color=blue]
      >I have a base (MustInherit/abstact) class that looks like:
      >
      > Public MustInherit Class BaseClass
      > End Class
      >
      > Then I have three derived classes:
      >
      > Public Class DerivedA
      > Inherits BaseClass
      > End Class
      >
      > Public Class DerivedB
      > Inherits BaseClass
      > End Class
      >
      > Public Class DerivedC
      > Inherits BaseClass
      > End Class
      >
      > Since each of these classes are handled differently I have three Shared
      > Overloaded Sub's
      >
      > Public Shared Sub BuildMessage(By Val Product As DerivedA)
      > End Sub
      >
      > Public Shared Sub BuildMessage(By Val Product As DerivedB)
      > End Sub
      >
      > Public Shared Sub BuildMessage(By Val Product As DerivedC)
      > End Sub
      >
      > When I compile I get:
      >
      > Overload resolution failed because no accessible 'BuildMessage' can be
      > called with these arguments:
      >
      > 'Public Shared Sub BuildMessage(Pr oduct As DerivedA)':
      > Option Strict On disallows implicit conversions from 'BaseClass' to
      > 'DerivedA'.
      > 'Public Shared Sub BuildMessage(Pr oduct As DerivedB)':
      > Option Strict On disallows implicit conversions from 'BaseClass' to
      > 'DerivedB'.
      > 'Public Shared Sub BuildMessage(Pr oduct As DerivedC)':
      > Option Strict On disallows implicit conversions from 'BaseClass' to
      > 'DerivedC'.
      >
      > This type of thing is done all the time with C# and C++. Since I am
      > relatively new to VB I am not sure how to get around this error. I would
      > rather not globally turn 'Option Strict' off, but if that is the only way
      > then I guess I must. Suggestions?
      >
      > Thank you.
      >
      > Kevin
      >[/color]


      Comment

      • AlanT

        #4
        Re: Overload resolution failed

        [color=blue]
        > This type of thing is done all the time with C# and C++. Since I am
        > relatively new to VB I am not sure how to get around this error. I would
        > rather not globally turn 'Option Strict' off, but if that is the only way
        > then I guess I must. Suggestions?
        >[/color]

        In general, Option Strict On, gives VB.Net 'C# equivalent' behaviour
        when it comes to casting. If the idiom works in C#/C++ then it should
        be achievable in VB.Net with strict On.

        What exactly are you doing when you get the overload error?

        It seems that you might be attempting something like

        dim p as BaseClass = new DerivedA;

        BuildMessage(p) ' expecting to use the DerivedA overload of
        BuildMessage

        I haven't been able to get that to work in either VB.Net or C# without
        an explicit cast.

        Is this what you attempting?


        Alan.

        Comment

        • Jim Wooley

          #5
          Re: Overload resolution failed

          > I have a base (MustInherit/abstact) class that looks like:[color=blue]
          >
          > Public MustInherit Class BaseClass
          > End Class
          > Then I have three derived classes:
          >
          > Public Class DerivedA
          > Inherits BaseClass
          > End Class
          > Public Class DerivedB
          > Inherits BaseClass
          > End Class
          > Public Class DerivedC
          > Inherits BaseClass
          > End Class
          > Since each of these classes are handled differently I have three
          > Shared Overloaded Sub's
          >
          > Public Shared Sub BuildMessage(By Val Product As DerivedA) End Sub
          >
          > Public Shared Sub BuildMessage(By Val Product As DerivedB) End Sub
          >
          > Public Shared Sub BuildMessage(By Val Product As DerivedC) End Sub
          >
          > When I compile I get:
          >
          > Overload resolution failed because no accessible 'BuildMessage' can be
          > called with these arguments:
          >
          > 'Public Shared Sub BuildMessage(Pr oduct As DerivedA)':
          > Option Strict On disallows implicit conversions from 'BaseClass' to
          > 'DerivedA'.
          > 'Public Shared Sub BuildMessage(Pr oduct As DerivedB)':
          > Option Strict On disallows implicit conversions from 'BaseClass' to
          > 'DerivedB'.
          > 'Public Shared Sub BuildMessage(Pr oduct As DerivedC)':
          > Option Strict On disallows implicit conversions from 'BaseClass' to
          > 'DerivedC'.
          > This type of thing is done all the time with C# and C++. Since I am
          > relatively new to VB I am not sure how to get around this error. I
          > would rather not globally turn 'Option Strict' off, but if that is the
          > only way then I guess I must. Suggestions?[/color]

          Since you want the method to be shared, you can't declare it in the base
          class as MustInherit or Overridable. Thus, you are likely putting each BuildMessage
          method inside the respective derived classes. If so, you may want to alter
          their signature to:

          Public Class DerivedA
          Inherits BaseClass
          Public Shared Sub BuildMessage(By Val Product as BaseClass)
          If TypeOf Product Is DerivedA Then
          'Process Product
          Else
          Throw New InvalidArgument Exception
          End If
          End Sub
          End Class

          Then in your calling code you can do the following:

          Dim Product as new DerivedA
          DerivedA.BuildM essage(Product)

          Jim Wooley



          Comment

          Working...