Class Inheritance

PoisonousFish

Member
Joined
Jan 24, 2009
Messages
8
Programming Experience
1-3
Okay I am playing around with classes in VB.net 2005

I have defined two classes in my current project. Class 1 and Class 2.

Public Class Class1
Public Sub New()

End Sub

Sub fun()

End Sub
End Class

Public Class Class2
Public Sub New()

End Sub

Sub fun()

End Sub
End Class


I tried to get class2 to inherit class1 by writing this code in the class2 file:

Public Class Class2 Inherits Class1
Public Sub New()

End Sub

Sub fun()

End Sub
End Class

But it did not work. What am I doing wrong? Thanks for your time.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
VB.NET:
Public Class Class2
    Inherits Class1
I'm surprised you didn't get that when you read the documentation for the Inherits statement. You did read the documentation for the Inherits statement, didn't you?

Also, you can't, or at least you shouldn't, define a 'fun' method in both the base and derived classes like that. If you define it in the base class then the derived class will inherit it, which is the whole point of inheritance. If you want to be able to provide a different implementation in the derived class then you should declare it Overridable in the base class and Overrides in the derived class.

Method names should always start with an upper case letter too, so 'fun' should be 'Fun'.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
Could I please have someone who is not insane reply to my thread...???
If you have the answer to your question, why do you need someone else to post? I've shown you exactly what you need to do and, if you'd taken the hint about reading the documentation, you would have seen this description of what I've demonstrated:
If used, the Inherits statement must be the first non-blank, non-comment line in a class or interface definition. It should immediately follow the Class or Interface statement.
What part is confusing you?
 

Pete (BSC)

Well-known member
Joined
Oct 19, 2007
Messages
89
Programming Experience
10+
Syntax

You could declare as such:

VB.NET:
Public Class MyClass : Inherits MyBaseClass

End Class

The idea that I follow for a base class is to put all my standard code and that if something comes up that doesn't follow or needs to be augmented to override it in the inherited class.

For example:

VB.NET:
Public Class MyBaseClass
    Public Overridable Sub ShowMessage()
           messagebox.show("Executing")
    End Function
End Class

Public Class MyClass: Inherits MyBaseClass
     Public Overrides Sub ShowMessage()
           'here you can use the base implementation or over ride.
           MyBase.ShowMessage()
     End Sub
End Class

Hope this helps.

P.S. jmcilhinney is a great contributor to this forum, implying that he is "insane" is furthest from the truth. Your question shows that you have some more reading to do on object oriented programming. I'm still learning and the people on this site have helped me, albeit a little stern at times; but, the help is always apprecited.

P.S.S I do not claim to be any more or less insane than jmcilhinney.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
You could declare as such:

VB.NET:
Public Class MyClass : Inherits MyBaseClass

End Class

The idea that I follow for a base class is to put all my standard code and that if something comes up that doesn't follow or needs to be augmented to override it in the inherited class.

For example:

VB.NET:
Public Class MyBaseClass
    Public Overridable Sub ShowMessage()
           messagebox.show("Executing")
    End Function
End Class

Public Class MyClass: Inherits MyBaseClass
     Public Overrides Sub ShowMessage()
           'here you can use the base implementation or over ride.
           MyBase.ShowMessage()
     End Sub
End Class

Hope this helps.

P.S. jmcilhinney is a great contributor to this forum, implying that he is "insane" is furthest from the truth. Your question shows that you have some more reading to do on object oriented programming. I'm still learning and the people on this site have helped me, albeit a little stern at times; but, the help is always apprecited.

P.S.S I do not claim to be any more or less insane than jmcilhinney.
Just note that the colon in VB is a way to put two logical lines of code on the same physical line. For instance, instead of doing this:
VB.NET:
MessageBox.Show("Hello")
MessageBox.Show("Goodbye")
you could do this:
VB.NET:
MessageBox.Show("Hello") : MessageBox.Show("Goodbye")
As such, this:
VB.NET:
Public Class MyClass : Inherits MyBaseClass

End Class
is really just this:
VB.NET:
Public Class MyClass
    Inherits MyBaseClass

End Class
massaged to look a bit different. I would suggest that colons be avoided completely, although this is a less evil example than most.

This is in contrast to C# where this:
VB.NET:
public class MyClass : MyBaseClass
{
}
is standard syntax with the colon being part of the single line of code, rather than existing specifically to allow two lines of code to exist side by side.
 

PoisonousFish

Member
Joined
Jan 24, 2009
Messages
8
Programming Experience
1-3
Thank you Pete, your response was helpful. It was just a mild syntax mistake I was making. All the reading I have done showed me the incorrect way to do it. The purpose of a forum is to ask questions and learn from others... Thanks for your time.

P.S. I was probably a bit harsh. I was in a bad mood last night and had a bit too much rum. rofl. Anyways I have added to both of your reps for taking the time. Thanks!
 
Last edited:
Top Bottom