fabulous Level: VB Guru

 Registered: 03-08-2002 Posts: 439
|
Re: how to declare inner class in vb.net
An inner class has the advantage of organizing your code logically. An example of this is the ListViewItemCollection class in the System.Windows.Forms namespace. This class serves no purpose outside a ListView so it is declared as an inner class.
To declare one, all you have to do is create the class definition inside the implementation like this:
Public Class HumanBeing
'code for human properties and methods
Public Class Brain
'code for brain here
End Class 'brain
End Class 'human |
In this imaginary example a brain can only be used in the context of a human. To create an instance of this class you would write code like the following:
| Dim br As New HumanBeing.Brain |
In some cases you may want the inner class to be private. This means that other classes know nothing about this inner class (private class). A common scenario I use it in is in Windows applications for forms that need to do extensive printing work. Each Form has its own different requirements and I create the inner class (normally called PrintHelper) to handle the printing process on a different thread. It receives the data to print and the process is then started on a new thread while the print form hides or unloads and normal processing can go on.
Other classes cannot create instances of this inner class and they do not know anything about them because its scope is private to the class in which it is defined.
Happy coding. 
____________________________
My boss is a Jewish Carpenter (Jesus Christ)

Brain Bench Certified VB.NET Developer
|