|
-
Dec 27th, 2002, 11:51 AM
#1
Thread Starter
New Member
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
-
Dec 27th, 2002, 12:04 PM
#2
PowerPoster
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.
-
Dec 27th, 2002, 12:11 PM
#3
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:
Public Class A
Public Overridable Sub F()
End Sub
Public Overridable Sub F(ByVal x As Integer)
End Sub
End Class
Public Class B
Inherits A
Public [b]Overloads[/b] Overrides Sub F(ByVal x As Integer)
End Sub
End Class
-
Dec 27th, 2002, 12:15 PM
#4
you need to specify the overloads keywords for those overloaded methods
VB Code:
Public Class A
Public Overridable Overloads Sub F()
End Sub
Public Overridable Overloads Sub F(ByVal x As Integer)
End Sub
End Class
Public Class B
Inherits A
Public Overloads Overrides Sub F(ByVal x As Integer)
End Sub
End Class
-
Dec 27th, 2002, 12:15 PM
#5
-
Dec 27th, 2002, 12:16 PM
#6
Sleep mode
Darn it! Edneeis just beat me to that
this maybe clearer Edneeis's is also will work perfect.
VB Code:
Public Class A
Public Overridable Overloads Sub F()
End Sub
Public Overridable Overloads Sub F(ByVal x As Integer)
End Sub
End Class
Public Class B
Inherits A
Public Overloads Overrides Sub F(ByVal x As Integer)
End Sub
End Class
-
Dec 27th, 2002, 12:18 PM
#7
Sleep mode
Cander as well
-
Dec 27th, 2002, 12:31 PM
#8
Thread Starter
New Member
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?
-
Dec 27th, 2002, 12:36 PM
#9
bug?
-
Dec 27th, 2002, 01:56 PM
#10
Sleep mode
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:
Public Overrides Sub F(ByVal x As Integer)
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.
-
Dec 27th, 2002, 02:10 PM
#11
Thread Starter
New Member
@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.
-
Dec 27th, 2002, 02:17 PM
#12
Sleep mode
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).
-
Dec 27th, 2002, 02:28 PM
#13
Thread Starter
New Member
@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.
-
Dec 27th, 2002, 02:38 PM
#14
Sleep mode
in another words........????
-
Dec 27th, 2002, 03:14 PM
#15
Thread Starter
New Member
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.
-
Dec 27th, 2002, 04:00 PM
#16
Sleep mode
Well , if you can't pose your question in VB.NET language ,it's better you stop programming
-
Dec 27th, 2002, 04:18 PM
#17
Thread Starter
New Member
@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
-
Dec 27th, 2002, 04:30 PM
#18
Sleep mode
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!
-
Dec 27th, 2002, 04:52 PM
#19
Sleep mode
-
Dec 30th, 2002, 10:11 AM
#20
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|