class/ stack question

I'm learning about classes in VB using the book" VISUAL BASIC LANGUAGE DEVELOPERS HANDBOOK" and they show an example of a stack class that is missing something or I'm not understanding something.

the StackItem class has code as follows:

Public Value As Variant
Public NextItem As StackItem      ' this I don't understand - what is this actualy setting up ????

the Stack class (just showing the push method for now) is as follows:

Public Sub Push(ByVal varText As Variant)
 Dim siNewTop As StackItem
 Set siNewTop = New StackItem
 siNewTop.Value = varText
 Set siNewTop.NextItem = sitop  '''' problem
 Set sitop = siNewTop
End Sub

'************
Main program:
dim stktest as Stack

sub testsub()
 set stktest = New Stack
 stktest.Push 1234
end sub
'*************

there is no declaration for sitop anywhere in the code and the program errors on that line:
 Set siNewTop.NextItem = sitop






pele4483Asked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Right...this:

     Public siTop as StackItem

would need to be declared in your Form or in a Module so that you have global reference to the top item in the stack.

=)
0
 
bruintjeCommented:
Hi pele4483,

if i read that code correctly

----------
Public NextItem As StackItem

is a public property like the Public Value as Variant, it is set up to hold something in the class
that something is the next stack item which get set in the push method you posted

this sets the siNewTop to a new stackitem
 Dim siNewTop As StackItem
 Set siNewTop = New StackItem

next you set the value of this stackitem to the value passed in
 siNewTop.Value = varText

then you set the previous stackitem top object to be the next stack item
 Set siNewTop.NextItem = sitop  '''' problem

and you promote the new stack item to the top
 Set sitop = siNewTop

in your next run sitop is passed to nextitem and the new stack item is the stack on top

so in your code on the first run pass a value like 123

the stack will look like
siTop                   123 <-- this was passed in
siTop.NextItem        0 <-- this was your initial value for nextitem because there was no next item

on the second run we pass in 456 and the stack looks like
siTop                   456
siTop.NextItem    123
----------

hope this helps a bit
bruintje
0
 
bruintjeCommented:
now i see the last part of you comment, you are correct siTop needs to be declared
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
pele4483Author Commented:
i declared siTop as a variant in the STACK class but I get object required error, I don;t know where and what to delcare it as.
0
 
bruintjeCommented:
i would expect it to be a Public siTop as StackItem, but that would look like a circular reference
0
 
bruintjeCommented:
0
 
pele4483Author Commented:
so why isn;t this a circular ref also

the StackItem class has code as follows:

Public Value As Variant
Public NextItem As StackItem      
0
 
bruintjeCommented:
difficult look at it from here, but if you need to have a sample of a easier to follow stack implementation that you can eventually use to debug your own sample
http://abstractvb.com/code/code1061.asp
0
 
pele4483Author Commented:
 Public siTop as StackItem worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.