Results 1 to 20 of 20

Thread: override confusing

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    11

    override confusing

    i have a class like this

    Public Class A
    Public Overridable Sub F()
    End Sub

    Public Overridable Sub F(ByVal x As Integer)
    End Sub
    End Class

    i want to inherit a class from Class A like this

    Public Class B
    Inherits A

    Public Overrides Sub F(ByVal x As Integer)
    End Sub
    End Class

    but i get a compile error that says

    sub 'F' cannot be declared 'Overrides' because it does not override a sub in a base class.

    when i rearrange Class A and change the location of the two overloaded methods it will be ok:

    Public Class A
    Public Overridable Sub F(ByVal x As Integer)
    End Sub

    Public Overridable Sub F()
    End Sub
    End Class

    can anybody tell me why it is so?

    thankX

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    That is weird. It seems that the compiler is stopping at the first sub, and not looking further. I don't know if this is by design, or if it is a bug. I will look for an answer, but can't guarantee it.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It must declare the unused overridable function with Overloads and thus requires it on that one was well, because if you add Overloads then it works:

    VB Code:
    1. Public Class A
    2.     Public Overridable Sub F()
    3.     End Sub
    4.  
    5.     Public Overridable Sub F(ByVal x As Integer)
    6.     End Sub
    7. End Class
    8.  
    9. Public Class B
    10.     Inherits A
    11.  
    12.     Public [b]Overloads[/b] Overrides Sub F(ByVal x As Integer)
    13.     End Sub
    14. End Class

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    you need to specify the overloads keywords for those overloaded methods

    VB Code:
    1. Public Class A
    2.     Public Overridable Overloads Sub F()
    3.     End Sub
    4.  
    5.     Public Overridable Overloads Sub F(ByVal x As Integer)
    6.     End Sub
    7. End Class
    8.  
    9. Public Class B
    10.     Inherits A
    11.  
    12.     Public Overloads Overrides Sub F(ByVal x As Integer)
    13.     End Sub
    14. End Class
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    darn..too slow..:-\
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Darn it! Edneeis just beat me to that
    this maybe clearer Edneeis's is also will work perfect.
    VB Code:
    1. Public Class A
    2.         Public Overridable Overloads Sub F()
    3.         End Sub
    4.  
    5.         Public Overridable Overloads Sub F(ByVal x As Integer)
    6.         End Sub
    7.     End Class
    8.  
    9.  
    10.  
    11.     Public Class B
    12.         Inherits A
    13.  
    14.         Public Overloads Overrides Sub F(ByVal x As Integer)
    15.  
    16.         End Sub
    17.     End Class

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Cander as well

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    11
    in Class A you are free to write down Overloads or omit it.
    we can always use Overloads Overrides in a derived class if the signature of the method is exactly like the signature of the method in base class. now AGAIN i ask why in the second version of Class A everything is ok (even without Overloads keyword) but in the first version something is wrong?

  9. #9
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    bug?

    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    the problem in your Code exists in Class B since you're trying to inherit Class A which contains 2 Subs with the same Sub names without telling your compiler that you want to overload(making many versions of the same Sub name) the Sub that has the same signature.Class B ,directs the Compiler to inherits Class A.Then , excute next line which is
    VB Code:
    1. Public  Overrides Sub F(ByVal x As Integer)
    2.  
    3. End Sub

    Now ,the Compiler found that you are inherting the sub called " F " ,he will go to Class A but found 2 Subs have the same names.
    in this case, you have to tell your compiler to take care because there are many Subs or Functions have the same name but I want the Sub with this identical signature.So just write Overloads.

    Edneeis's way is fine but if you want it more clear , add Overloads keyword to both Subs.

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    11
    @pirate

    so why compiler does not report any error if i write the class like this:

    Public Class A
    Public Overridable Sub F(ByVal x As Integer)
    End Sub

    Public Overridable Sub F()
    End Sub
    End Class

    it is all my question about.

  12. #12
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You do not have to use the Overloads keyword when you are defining multiple overloaded Subs in the same class. This is only required when inhertiance is applied(from another Class).

  13. #13

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    11
    @pirate

    i am not asking about overloading methods in the same class. what do you mean by saying that? i am asking about a behavior of vb.net compiler about overriding an overloaded method from a base class in a derived class.

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    in another words........????

  15. #15

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    11
    in another words i can say

    there are 10 kinds of people in the world. those who understand binary and those don't understand binary.

  16. #16
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Well , if you can't pose your question in VB.NET language ,it's better you stop programming

  17. #17

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    11
    @pirate

    it was already written in a plain way. if you read it carefully you didnot get a low level machine dependent message.
    by the way i assume this is a free of charge site to help people promote their knowledge not to say them quit programming. sooner or later i will get the answer to my PLAIN (not a binary one) question and i will share with those who still go on programming vb.net

  18. #18
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Well , I think I expalined everything about Overriding @ Overloading methods , but you need the whole day to understand what I POSTED.If it's difficult to understand then keep away boy!

  19. #19
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  20. #20
    New Member
    Join Date
    Dec 2002
    Posts
    2
    i tested the two versions of Class A and got that strange compile time error in the first version of Class A and no error in the second one. but in the last one compiler sends a warning message saying method shadowing. i dont know if shadows is the problem or not but i work on it and sent you the result, if any.

    daidaluus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width