Solved

Self Referencing Classes

Posted on 2007-04-04
13
244 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:BrianGEFF719
  • 7
  • 6
13 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18855884
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
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18858565
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18859025
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
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18859173
>>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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18859200
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18859229
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18859255
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
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18859273
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18859354
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
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18859401
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
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18859411
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18859424
PERFECTLY stated.
0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 18859493
Wonderful! This has been a great lesson in VB6 OOP.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now