Class with In a Class?

  • Thread starter Thread starter JoseM
  • Start date Start date
J

JoseM

In vb.net what is the benefit of having a class within a class?

If anyone can pass the knowledge it would greatly be appreciated.
 
It's a very broad question but possible reasons include:
A property of a class being another class
Using inherits to extend behaviour - eg create a base class 'Individual'
that handles Name and Addess functionality

This can then be inherited to a customer class, a contact, a patient etc

Have alook at the topic Visual Basic Language Concepts - Inheritance in
MSDN for more
 
* (e-mail address removed) (JoseM) scripsit:
In vb.net what is the benefit of having a class within a class?

If anyone can pass the knowledge it would greatly be appreciated.

In addition to the other reply: Have a look at the .NET framework class
library to see in which situations nested classes make sense.
 
JoseM,
As the others suggest, can you be a little more specific in what you are
asking?


Do you mean a nested class?

Public Class Class1
Private Class Class2
End Class
End Class

Having Class2 nested inside of Class1 is a form of Encapsulation, I would
use this when Class2 is an implementation detail of Class1 and Class2 is not
needed anyplace else, such as the Node class of a LinkedList class, where
the Node represents the "links" in the LinkedList.

Something like (greatly simplified):

Public Class LinkedList

Private Class Node
Public Data As Object
Public Next As Node
Public Previous As Node
End Class

Private m_head As Node
Private m_tail As Node

Public Sub Add(data As Object)
Dim node As New Node
node.Data = data
node.Next = m_head
node.Previous = Nothing
m_head = node
End Sub
End Class

The Node class is nested inside of LinkedList as it is not useful outside of
the context of a LinkedList. Having Node private to LinkedList ensures that
only LinkedList is able to use the Node class.

Hope this helps
Jay
 
Back
Top