Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

class/ stack question

Posted on 2006-05-03
9
Medium Priority
?
224 Views
Last Modified: 2010-05-01
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






0
Comment
Question by:pele4483
  • 5
  • 3
9 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16597495
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
 
LVL 44

Expert Comment

by:bruintje
ID: 16597517
now i see the last part of you comment, you are correct siTop needs to be declared
0
 

Author Comment

by:pele4483
ID: 16597600
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

Expert Comment

by:bruintje
ID: 16597669
i would expect it to be a Public siTop as StackItem, but that would look like a circular reference
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16597682
0
 

Author Comment

by:pele4483
ID: 16597789
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
 
LVL 44

Expert Comment

by:bruintje
ID: 16597866
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 120 total points
ID: 16598887
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
 

Author Comment

by:pele4483
ID: 16614351
 Public siTop as StackItem worked.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question