Implementing a sub class

B

Brian Patrick

Hi - I'm trying to implement what a seems to be a very simple concept, but I
can't figure out how exactly it is supposed to be done.

I want to define a class that has sub classes, so I'll explain in a very
simplified manner:

The main class will be called "MyAppClass". This class will have two
methods - "GenericMethod1" and "GenericMethod2". Then I want to have two
sub classes inside MyAppClass called "Section1Class" and "Section2Class".
Section1Class has a method called "DoSection1Work", and Section2Class has a
method called "DoSection2Work"

Then, user of MyAppClass would use it in the following way:

Dim obj as new MyAppClass

obj.GenericMethod1()
obj.Section2Class.DoSection2Work()

Please tell me how I implement this "sub class" idea.

Thanks,
Brian
 
C

Charlie Brown

Create a property in your main class which exposes the sub class.

You can nest classes together like so....

Public Class SectionClass1

...class methods and properties

Public Class SectionClass2

Public Sub DoSomething()
............
End Sub

End Class

End Class

DoSomething method can be called by

SectionClass1.SectionClass2.DoSomething()
 
B

Brian Patrick

This implementation does not work - when I try it, the DoSomething() method
is not accessible.
 
B

Brian Patrick

This is the way I'm doing it now, and it works - however, how do I keep the
sub class from being instantiated from the outside? I only want the class
creatable via the parent class. Using the "Protected" keyword doesn't work.
 
R

rowe_newsgroups

This implementation does not work - when I try it, the DoSomething() method
is not accessible.

You need to instantiate SectionClass2 before you can access
DoSomething(). Alternatively you could use a singleton implementation
(single instance self-instantiating classes) on both of the nested
classes. Here's Jon Skeet's article on a few different patterns (in
C#) http://www.yoda.arachsys.com/csharp/singleton.html

Thanks,

Seth Rowe
 
S

scenic_man

Sub class, as in:
Acheron, Astute, Delta, Dolphin, Echo, Golf, Harushio, Los Angeles,
Ming, Ohio, Oscar, Oyashio, Romeo, Triomphant, Type 206A, Typhoon,
Vanguard, Whiskey, Yuan -- other?

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top