-
Re: .NET subclassing question (solved)
Paul,
Ayour suggestion does almost exactly what I want, however there seems to
be another solution, which I discovered (*embarrassed*) by accident: Nested
Classs.
Class Customer
'stuff here like Name etc
friend class Orders
'stuff here for orders
end class
end Class
By the above, I can actually limit completely the ability for clients to
dimension the class let alone try to instansiate it!
Thanks for your help
Robert
"Paul Mc" <paulmc@nospam.thehub.com.au> wrote:
>
>G'day Robert.
>
>>I've had a play with .NET and perused MSDN, however I can't seem to find
>>a way to achieve the same result (class heirarchy). Keeping in mind that
>>I do not wish to allow clients to directly access the "SomeSubClass", but
>>rather only via its super.
>
>OK there are a few things to think about here.
>
>First, you need the Child class - call it cOrders - to be declared public.
>That way, clients will be able to use the class.
>
>So, we have
>
>Public Class cOrders
>End Class
>
>Second, you want cOrders to be a child of the Parent class - call it cCustomer
>- and only anstantiable via friend access.
>
>So, now we have:
> Public Class cOrders
> Friend Sub New()
> 'Initialise stuff
> End Sub
> End Sub
>
>The Public scope of tyhe class allows clients to read and use the cOrders
>class, however the Friend scope of your Instantiator (ie Sub New) means
that
>only objects in your assembly can instantiate it. Thus, if you are creating
>a DLL for instance, clients that reference your DLL will *not* be able to
>get an instance of cOrders without you supplying a method having a return
>type of cOrders.
>
>Now, this does *not* limit the cOrders class to instantiation via one specific
>"Parent" class - say, cCustomer. To do that, things will need to be a bit
>more tricky - the first option that springs to my mind (hopefully someone
>out there chimes in with a more elegant solution) works on the following
>theory:
>
>Nest the child class (cOrders) so it has access to private members of the
>parent (cCustomer).
>
>In the parent have some private flag/token letting the child know if it
is
>OK to instantiate.
>
>Here is an example implementation:
>
>Public Class cCustomer 'The Parent class
> Private mbToken As Boolean = False
> Private mOrders As cOrders
>
> Public ReadOnly Property Order() As cOrders
> Get
> If mOrders Is Nothing Then
> mbToken = True
> mOrders = New cCustomer.cOrders(Me)
> mbToken = False
> End If
>
> Return mOrders
> End Get
> End Property
>
> Public Class cOrders ' Child class is now nested
> Friend Sub New(ByRef Customer As cCustomer)
> If Not Customer.mbToken Then
> Throw New ApplicationException("Orders Class May Only Be Instantiated
>From Customer Instance")
> Exit Sub
> Else
> 'Initialise stuff
> End If
> End Sub
> End Class
>End Class
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks