BrianGEFF719
asked on
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
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
ASKER
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
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
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. =)
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. =)
ASKER
>>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
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
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++...
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++...
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.
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.
ASKER
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.
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.
ASKER
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???
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???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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?
Set RootNode = Nothing 'DELETES THE ROOT NOT AND EVERYTHING IT IS LINKED TO...
Is that correct?
PERFECTLY stated.
ASKER
Wonderful! This has been a great lesson in VB6 OOP.
But who told you can't do that in VB6?
Here is a stack in VB6:
https://www.experts-exchange.com/questions/21298882/a-stack-using-a-linked-list.html#13209219