Self Referencing Classes

Hello,
I'm very familiar with VB6 and not .NET, in VB6 you cannot create a class that has as a member the class its self. What i'm trying to do to get familiar with .NET is build a simple Queue class. Please take a look at the psuedocode below and tell me if this will or will not work.

** I do not need fully working code, I just need to know if the following is even possible in .NET **

For example:

Node Class
 '//I"M USING PUBLIC MEMBERS SIMPLY FOR TESTING!!!!
 Pubic nextNode as Node
 Public Data as Long

Queue:
 Private root as Node
 Private Count as integer
 public sub enqueue(data as long)
    dim theNode as Node
    set theNode = root
    while (theNode.value = 0)
     set theNode = theNode.nextNode
    wend
    theNode.value = data
    dim x as new Node
    theNode.nextNode = x    
 end sub
LVL 19
BrianGEFF719Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Sure it's possible...

But who told you can't do that in VB6?

Here is a stack in VB6:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21298882.html#13209219
0
BrianGEFF719Author Commented:
Hi Idle_Mind, thanks for that.

But for example how would you traverse the stack?

Would this be correct FOR YOUR EXAMPLE:

while (not isnothing(theNode.nextNode))
 set theNode = theNode.nextNode
wend



0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes...that's pretty much it.

Here is a "PopAll" sub that would go into class Stack:
(assuming primitive in the Stack)

Public Sub PopAll()
    While Not (HeadNode Is Nothing)
        Dim value As Variant
        value = HeadNode.Item
        Debug.Print value
               
        Set HeadNode = HeadNode.NextNode
       
        StackCount = StackCount - 1
    Wend
End Sub

The VB.Net version of these would basically be the same...just without the "set" keyword.

This is standard Abstract Data Type stuff...just about every computer science student in the world has to build a linked list, stack and queue using this methodology in some language that supports OOP.  =)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

BrianGEFF719Author Commented:
>>This is standard Abstract Data Type stuff...just about every computer science student in the world has to build a linked list, stack and queue using this methodology in some language that supports OOP.  =)

I've taken a Data Structures Class and an Advanced Data Structures Class, however, in my university its all done in C++. So i'm very familiar with how this stuff works.

Which brings me to my next question. The code you've posted for PopAll, seems to have a memory leak. You change the head node to currentnode.nextnode, which is fine, but you never set the current node = nothing. Correct me if I'm wrong but should you do something like:

While Not (HeadNode Is Nothing)
        Dim value As Variant
        dim currentNode as Node
        value = HeadNode.Item
        Debug.Print value
       
        Set currentNode = HeadNode
        Set HeadNode = currentNode.NextNode
        Set currentNode = Nothing 'clean up
       
        StackCount = StackCount - 1
    Wend
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
In C++ this would create a leak as you would have lost all references to it.

In VB6 and MANAGED .Net apps (VB.Net/C#) though, the "lost" node would be garbage collected automatically.

There is nothing wrong with explictly setting it to "Nothing" though as you have.

It depends on ~what~ is in the node really.  If you are dealing with UNMANAGED structures (sometimes returned by Win APIs) then you need to manually track and dispose of them as in C/C++.  The VB6 and .Net world is very different from C++...
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
So are you working in VB6 or VB.Net?

In VB.net you could create a private "inner class" of StackNode inside the Stack class and completely hide that from the end user.

VB6 is very limited with respect to OOP but as shown above the very basics of it are actually there.
0
BrianGEFF719Author Commented:
Hi Idle_Mind,
I'm very familar with C++ OOP. However, I'm pretty experienced with VB6, my knowledge of of VB6 OOP is really lacking. For now i'm doing this in VB6.
0
BrianGEFF719Author Commented:
And how does that not cause an infinite loop?

Node Class:

Dim Data as Variant
Dim NextNode as Node '// Should this cause another node to be instanciated, and would this then cause another node to be instanciated and so on and son on???
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
An instance is only created if you have the "New" keyword in there.

So this is fine:

    ' Node Class
    Dim Data as Variant
    Dim NextNode As Node

because NextNode is not creating another instance of "Node" automatically.  Initially it points to "Nothing".  =)

We either point it to an already existing Node or manaully create a new Node and point it to that.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrianGEFF719Author Commented:
I'm just trying to wrap my head around this VB6 OOP, I'm just used to doing stuff like this with Pointers to classes, thats why VB6 OOP is so weird.

I think I'm understanding it now.

Class:
Dim Data as Variant
Dim NextClass as Class

Dim x as Class 'Basically a null pointer to an object of type class
Set x = New Class 'Now x points to an instance of class

x.NextClass SHOULD BE == NOTHING.
x.NextClass = New Class
x.NextClass.NextClass SHOULD BE == NOTHING.

x.NextClass = Nothing 'Both x.NextClass and x.NextClass.NextClass are automatically cleaned up by garbage collector.

Is this all correct?
0
BrianGEFF719Author Commented:
In which case if my logic is correct, if you wanted to pop an entire stack without returing the data you could just do:

Set RootNode = Nothing 'DELETES THE ROOT NOT AND EVERYTHING IT IS LINKED TO...


Is that correct?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
PERFECTLY stated.
0
BrianGEFF719Author Commented:
Wonderful! This has been a great lesson in VB6 OOP.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.