Re: Subclassing VBA/VB6/.Net
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Fri, 30 Dec 2005 18:11:45 -0500
<zoneal@xxxxxxxxx> wrote in message
news:1135976248.354571.303910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>i want to create a class that has properties that are classes,
> themselves, that also have properties (much like
> userform.controls.count).
>
> i've never had the need to do this, but now i want to know the best way
> to go about this. do i go as far as creating a new class module and
> instantiating it within another class? or should i use a user-defined
> type (vb6) or a structure (.net)?
> I'm using VB6 for now.
Avoid UDTs. They just don't work well at all if trying to do things in an
object-oriented way.
You'd have 2 class modules. The next decision (actually, this should
probably be the first decision) is whether these 2 class modules are going
to be in a DLL project or part of your standard EXE project. If in a DLL
project, you'd probably want the first class module's Instancing property to
be 5 - MultiUse (although it could also be 6 - GlobalMultiUse, but I
personally would not recommend that). You'd want Instancing of the 2nd class
to be 2 - PublicNotCreatable. If these classes are in a standard EXE
project, then Instancing must, and can only, be 1 - Private.
In Class1, you'd create an instance of Class2 in the Initialize event. For
example (Class1 code):
Private Sub Class_Initialize()
Set m_oClass2 = New Class2
End Sub
The variable m_oClass2 would be declared in the General Declarations section
of Class1 as follows:
Option Explicit
Private m_oClass2 As Class2
In Class1, you'd have a property Get procedure to expose Class2 as such
Public Property Get Class2() As Class2
Set Class2 = m_oClass2
End Property
That's really about it. Note that there's no corresponding Property Set.
You want the Class2 property of Class1 to be read-only. IMO, a major flaw in
VB.NET is that you can't have a read-only property. Although, I believe
VB2005 *finally* corrects this oversight. It took MS long enough. 6 years
to "restore" what I think is necessary and important functionality of
classes. From what I've seen so far of VB2005, I like it OK...still not as
much as VB6, but it's getting better. I may even start using VB2005 more and
more instead of VB6. Egads! I never thought I'd say something so
blasphemous. <g>
Anyway. to use any Public properties or methods of Class 2 in your EXE, all
you need to do is instantiate Class1, as such
Dim oClass1 As Class1
Set oClass1 = New Class1
PropertyValue = oClass1.Class2.PropertyName
You could even do something like this:
Dim oClass1 As Class1
Dim oClass2 As Class2
Set oClass1 = New Class1
Set oClass2 = oClass1.Class2
PropertyValue = oClass2.PropertyName
You could NOT, however, do this:
Dim oClass2 As Class2
Set oClass2 = New Class2
This is because of the PublicNotCreatable setting for the Instancing of
Class2. Class2 is exposed as Public for use in the EXE, but the EXE itself
cannot instantiate Class2. This is why you instantiate Class2 in the
Initialize event of Class1 (and you may not even want to instantiate Class2
in that event; I just did it there for simplicity, but I would recommend
that's where you instantiate Class2 unless you have a specific reason not
to). If Instancing of Class2 were also MultiUse (or GlobalMultiUse) OR if in
a standard EXE and both classes are Private, then you lose the encapsulation
of Class2 in Class1 (because you'd be able to instantiate Class2 directly,
rather than only being able to get a reference to Class2 from Class1).
Also, I would recommend cleaning up in Class1 as follows:
Private Sub Class_Terminate()
Set m_oClass2 = Nothing
End Sub
If Class2 instantiates any objects, those objects should be cleaned up in
the Terminate event of Class2. Strictly speaking, this shouldn't be
necessary, but I think it's a good idea to always explicitly clean things up
yourself rather than just trust that VB is going to do it for you.
If you need more help than that or if anything isn't clear, just say so and
either I or someone can post more complete, working code. But except for
defining all the properties and methods of both classes, that's really about
it. It's not really too difficult at all if you understand the concept of
classes.
--
Mike
Microsoft MVP Visual Basic
.
- References:
- Subclassing VBA/VB6/.Net
- From: zoneal
- Re: Subclassing VBA/VB6/.Net
- From: MikeD
- Re: Subclassing VBA/VB6/.Net
- From: zoneal
- Subclassing VBA/VB6/.Net
- Prev by Date: Re: Text Box question
- Next by Date: Re: Delete folder
- Previous by thread: Re: Subclassing VBA/VB6/.Net
- Next by thread: Run treeview from top to bottom after drag and drop
- Index(es):
Relevant Pages
|