?
Solved

Self Referencing Classes

Posted on 2007-04-04
13
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 86

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 86

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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 86

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
 
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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 86

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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