Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Structures and Pointers 1

Status
Not open for further replies.

WoundEdGoat

Programmer
May 10, 2002
39
CA
What are the equivalent forms of structures and pointers in VB (if any)? I know how to use them in C++ and would like to know if they exist/how to use them in VB.
 
Actually... Now that I've thought a little on it, what I'd really like to know is how to make a linked list in VB.
 
Look at a Collection. It has access via, For/Each, subscript and key, and has ADD and REMOVE methods. Forget pointers as you know them in C++. The nearest thing is object references. Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Another approach to building a linked list is to use a Type Structure and an Array.

Create a Type Structure with the various node attribute items, and then add a NextPtr as the last field in the Type Structure.

Type LISTNODE
Dim fld1 as string
Dim fld2 as string
...
Dim NextPtr as Long
End Type

Also create a Dynamic Array of Type ListName

Dim TheWholeList() as LISTNODE

As you add nodes to the list, you can use the ReDim Preserve, and then use the NextPtr as the array subscript of the next element in the List.

You may also want to have a HeadPtr defined as another variable.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
You can also define a class for a node, and include one or more pointers to the node's class inside the class definition. Then use the New keyword to create new nodes,
and link them with properties:
For example (This module is named clsNode):

private mNextNode as clsNode
'Other variables you need for the class...

Property Set NextNode(next as clsNode)
Set mNextNode = next
End Property

Property Get NextNode(next as clsNode)
NextNode = mNextNode
End Property

'--------------------
'In the calling code:
dim node1 as clsNode
dim node2 as clsNode

set node1 = New clsNode
set node2 = New clsNode

Set node1.NextNode = node2
'This creates a circular reference, which you
'probably don't want to do...
Set node2.NextNode = node1
 
'Sorry, I didn't test this code first, that should be:
Property Get NextNode() As clsNode
Set NextNode = mNextNode
End Property
 
There isn't a very good parallel in VB to what you want to do. The closest you'll get is probably 'collections' or 'records', but there's some manual maintenance involved, and it's a pretty inefficient way of treating non-homogenous data.

You might consider dynamic arrays, but you'll still have the maintainence problem.

This is probably one of the bigger drawbacks to VB vs. C/C++. --------------
Regards, Kevin
 
Well thanks the help guys, but I've decided to use C++ for my program instead. Thanks again.
 
Wise choice, weedhopper. <deep, shimmering gong sound> --------------
Regards, Kevin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top