emulation of nested Public array() in Class?

D

Dick Watson

I understand that I can't declare a Public array in a class module. The help
says that I can use As Variant property procedures to emulate this. I guess
I vaguely understand what that means, but even that I'd like to find an
example of. More to the point, I want to nest these objects and still be
able to readily access nested member variables. (Example below.)

I would appreciate any thoughts and pointers to examples/techniques of how
to do this in VBA! Thanks in advance!

Code frags:
-----
Attribute VB_Name = "testnest"

Sub testnodes()

Dim objMyNodes(1) As Node

For x = 0 To UBound(objMyNodes)
Set objMyNodes(x) = New Node
objMyNodes(x).varMyData = "some data"
objMyNodes(x).testthis (0)
Next

Rem this kind of deep reference is really what I'm after
MsgBox objMyNodes(0).objMyNodes(1).objMyNodes(2).varMyData

End Sub
-----
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Node"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Public varMyData As Variant
Rem compiler gags on this; says Arrays cannot be public
Public objMyNodes() As Node

Sub testthis(intDepth)

ReDim objMyNodes(1)

For x = 0 To UBound(objMyNodes)
Set objMyNodes(x) = New Node
If intDepth < 3 Then
objMyNodes(x).testthis (intDepth + 1)
Else
objMyNodes(x).varMyData = "some data " & Rnd(0)
End If
Next

End Sub
 
T

Tushar Mehta

The way you are going about this is by and large contrary to the intent
of OOPS. You should *hide* the details of the implementation not seek
ways to move the responsibility of the maintenance to the consumer.
Below is an untested architecture for a binary tree.

Declare a Node class:
Dim LeftNode as Node, RightNode as Node, _
iNodeData as Integer
property get NodeData() as integer
nodedata=iNodeData
end property
property let NodeData(uNodeData as integer)
iNodeData = uNodeData
end property
property get LeftChild() as Node
set LeftChild=LeftNode
end property
'...same for RightChild
public function addAChild(LeftChild as Boolean)
if leftchild then
if leftnode is nothing then
set leftnode=new node
set addachild=leftnode
else
'return some kind of error
end if
else
if rightnode is nothing then
set rightnode =new node
set addachild=rightnode
else
'return some kind of error
end if
end if
end sub
'a more useful function is to add a data value based on the value of the
existing nodes. This is very useful for sorting
public sub AddDataToTree(newData as integer)
if dataval>newData then
if leftnode is nothing then
set leftnode =new node
leftnode.Nodedata=newData
else
leftnode.adddatatotree(newData)
end if
else
if rightnode is nothing then
set rightnode =new node
rightnode.Nodedata=newData
else
rightnode.adddatatotree(newData)
end if
end if
end sub

While a binary tree is a special tree with special properties, one can
easily create a tree in which a node supports an arbitrary number of
children.

Dim Children() as Node, NbrChildren as integer, _
iNodeData as Integer
property get NodeData() as integer
nodedata=iNodeData
end property
property let NodeData(uNodeData as integer)
iNodeData = uNodeData
end property
property get OldestChild() as Node
if nbrchildren=0 then
else
set OldestChild=Children(0)
end if
end property
'...similar for youngestchild
property get AllChildren() as Node() 'needs VB6
allchildren=children
end property
public function addAChild()
redim preserve children(nbrchildren)
set children(ubound(children))=new node
set addachild=children(ubound(children))
nbrchildren=nbrchildren+1
end sub
sub class_initialize()
nbrchildren=0
end sub

Now, if someone wants the oldest child of the oldest child, one would
use
Dim StartOfFamily as Node
'Create tree data before accessing it below
msgbox StartOfFamily.OldestChild.OldestChild.NodeData

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2005
 
D

Dick Watson

You've given me some great ideas here. I'll have to go play with this
some... Thanks!
 

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