From: mickey on
Hi Group
I think that what I would like to do is not possible.
I would like to create a class with four possible sub classes which must
inherit data from the master class.
In the first class I would like to do some work which would then instantiate
one of the four sub classes without calling New in the first class.
The sub classes each has the potential for holding large amounts of data,
there for I don't want to instantiate them if not necessary.
When I do call the chosen sub class from the first class, I would like it to
inherit some of the data that the first classes created.

I hope this makes sense and I hope it is possible.

Thanks
Mickey

From: Family Tree Mike on
On 2/28/2010 10:04 AM, mickey wrote:
> Hi Group
> I think that what I would like to do is not possible.
> I would like to create a class with four possible sub classes which must
> inherit data from the master class.

OK...

> In the first class I would like to do some work which would then
> instantiate one of the four sub classes without calling New in the first
> class.

This "first class" is one of the four subclasses of "master class"? If
that is true, then the other subclasses have nothing to do with the New
in the "first class".

It sounds as "first class" is a factory class
(http://www.dofactory.com/Patterns/PatternFactory.aspx) for the other
three classes.

> The sub classes each has the potential for holding large amounts of
> data, there for I don't want to instantiate them if not necessary.
> When I do call the chosen sub class from the first class, I would like
> it to inherit some of the data that the first classes created.
>
> I hope this makes sense and I hope it is possible.
>
> Thanks
> Mickey

If I understand you as described above, then this is possible. I'm not
sure why the "first class", which seems like a factory class would
inherit from the same base of the classes that it instantiates. A
typical construct might be:

class vehicle
end class

class car
inherits vehicle
end class

class truck
inherits vehicle
end class

class FordFactory
function Create_F150 as truck
end function
function Create_Focus as car
end function
end class

--
Mike
From: Phill W. on
On 28/02/2010 15:04, mickey wrote:

> I would like to create a class with four possible sub classes which must
> inherit data from the master class.

Subclasses will have access to anything in the base class defined as
Public, Protected or Friend (C#: public, family or assembly).

> In the first class I would like to do some work which would then
> instantiate one of the four sub classes without calling New in the first
> class.

Only possible if your base class exposes a niladic constructor (i.e.
zero arguments). Subclasses [should] always call New on their base class.

> The sub classes each has the potential for holding large amounts of
> data, there for I don't want to instantiate them if not necessary.
> When I do call the chosen sub class from the first class, I would like
> it to inherit some of the data that the first classes created.

Two suggestions:
(1) Pass the "inherited" data to a method on the subclass. Yuck.

(2) Create a Shared (a.k.a. Static) method on the base class that gets
you an instance of the /required/ subclass. Then invoke a "Do
Processing" method on that. This method can, in turn, call [Protected]
methods on the base class.

Friend [MustInherit] Class Base1
Public Shared Function GetSubclass( ... )
If ( condition ) Then
Return New Subclass1( ... )
End If
Return Nothing
End Function

Public [MustInherit|Inheritable] Function DoWork() As ...
Return ...
End Function

End Class

Friend Class Subclass1
Inherits Base1

Public [Overrides] Function DoWork() As ...
' Do sub-class specific work
Return ...
End Function

End Class

HTH,
Phill W.
 | 
Pages: 1
Prev: Problem Removing Item
Next: Setup in vs 2010