|
-
Sep 23rd, 2008, 11:31 PM
#1
Thread Starter
Addicted Member
[2005] Manual pointers in VB
Hello everyone,
Been a while but I have a question about creating manual pointers and the logic needed to stick a pointer on the head of the list and the tail of the list. I have the general idea but the logic I am trying to use just isn't working out. Please observe.
Code:
Public Class Form1
Public PointerTracker As Nodei
Public FrontP As Nodei
Public TempP As Nodei
Dim GetData As String
GetData = txtGetData.Text
Dim Node As New Nodei
Node = Nothing
Node = New Nodei
Node.DataHolder = GetData
Node.IndexP = PointerTracker
PointerTracker = Node
Public Class Nodei
Public DataHolder As String
Public IndexP As Nodei
End Class
The simple code above will produce a linked list. I have declared the additional variables to hold the positions temp and front but in all honesty I can't find the logic that will allow me to put conditions against any of the class Nodei that I have created without receiving object null reference error.
Yep this is homework. Don't need answers as much as suggestions. Been a while since I have moved around an IDE and am new to data structures. Any ideas?
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Sep 25th, 2008, 12:10 AM
#2
Thread Starter
Addicted Member
Re: [2005] Manual pointers in VB
I am still chopping away at this and I can't even find words to convey my frustration with trying to get pointers and linked lists to work in VB but I am solving it 1 problem at a time. I am now faced with an if statement that is being ignored and I can't figure out why because VS isn't throwing me any exceptions.
Code:
Code:
If FrontP.Data Is Nothing Then
MessageBox.Show("thank god")
FrontP.Data = GetData
FrontP.NextP = Nothing
TempP = FrontP
When debugging the following code is simply being stepped right over. I called a new instance of my node class, being FrontP, and when I debug and step through I can not seem to make Vs evaluate FrontP as being nothing, empty, null, or 0. Any suggestions?
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Sep 25th, 2008, 03:29 AM
#3
Hyperactive Member
Re: [2005] Manual pointers in VB
i am sure i am going to end up with egg on my face, but, if you want a linked list why don't you just use LinkedList (systems.collections.generic)?
I've been known to be wrong  ...
-
Sep 25th, 2008, 08:02 AM
#4
Re: [2005] Manual pointers in VB
Cause it's homework, as he said.
So when you step onto the If statement, and use Shift+F9 or the tooltip to look at the value, what do you see?
Also, what is FrontP.Data? I see that you have declared FrontP to be of type Nodei in the first snippet, but there is no Data member or property to that class in the first snippet.
My usual boring signature: Nothing
 
-
Sep 25th, 2008, 10:29 AM
#5
Hyperactive Member
Re: [2005] Manual pointers in VB
so i was right about the egg, then?
dude, you gonna need explicit constructors so that class instances know who their parents are, and destructors, if you want to prevent removed items from breaking the chain.
as its your homework, i don't know whether or not the code below goes too far, but if you think it does, then DON'T read it .. .. .. innit?
vb.net Code:
' non mea culpa...
Public Sub DoNodes()
Dim n As New Nodesi
Dim nn As New Nodesi(n)
MsgBox(n.GetData & vbNewLine & nn.GetParent.GetData)
End Sub
Public Class Nodesi
Private _parent As Nodesi
Public Sub New()
End Sub
Public Sub New(ByVal parent As Nodesi)
_parent = parent
End Sub
Public Function GetData() As String
Return CStr(Me.GetHashCode)
End Function
Public Function GetParent() As Nodesi
If _parent Is Nothing Then Return Me Else Return _parent
End Function
End Class
I've been known to be wrong  ...
-
Sep 25th, 2008, 11:14 AM
#6
Re: [2005] Manual pointers in VB
I'm assuming you're trying to set up a line where one object references another and can continue on into infinity. If so, look over this:
VB Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tailObject, middleObject, leadObject As Nodei
leadObject = New Nodei
leadObject.DataHolder = "Lead"
middleObject = New Nodei
middleObject.DataHolder = "Middle"
middleObject.Follows = leadObject
tailObject = New Nodei
tailObject.DataHolder = "Tail"
tailObject.Follows = middleObject
End Sub
Public Class Nodei
Public DataHolder As String
Public Follows As Nodei
End Class
-
Sep 25th, 2008, 01:51 PM
#7
Re: [2005] Manual pointers in VB
 Originally Posted by bigMeUp
so i was right about the egg, then?
Not even enough egg to make a reasonable breakfast, but since you asked for it, I supplied what was available
My usual boring signature: Nothing
 
-
Sep 28th, 2008, 02:47 PM
#8
Thread Starter
Addicted Member
Re: [2005] Manual pointers in VB
Thanks for all of the help guys. I'm still rolling on this. This is what I have so far. I just need to understand the structure itself before I can use the pointers to their fullest potential. This is what I have so far. It is acting like a queue and its suppose to, sorry I omitted that in my first post:
General From wide declarations:
Code:
Public GetData As String
Public FrontP As Node
Public RearP As Node
Button_click event for adding a new node:
Code:
Dim CurrP As New Node
Dim Newnode As New Node
CurrP = Newnode
CurrP.Data = GetData
CurrP.NextP = Nothing
If RearP Is Nothing Then
RearP = CurrP
FrontP = CurrP
Else
RearP.NextP = CurrP
RearP = CurrP
End If
Now I'm working on scanning the list for the first node, once the current data equals the front data I am deleting from the front. This section is not working thus far.
Code:
Do Until CurrP Is FrontP
CurrP.NextP = RearP
CurrP = RearP
If FrontP Is CurrP Then
Exit Do
End If
Loop
I'm still working on it. Linked Lists have to be the hardest thing for me to fully grasp since I have started coding. **** is giving me a headache.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Sep 29th, 2008, 09:34 AM
#9
Re: [2005] Manual pointers in VB
If you want, try this approach. It's a little more digestible for what you want.
VB Code:
Public Class Node
Public Data As String
Public NextP As Node
End Class
Private myNodes As Generic.List(Of Node)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
myNodes = New Generic.List(Of Node)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newNode As New Node
If myNodes.Count > 0 Then
myNodes(myNodes.Count - 1).NextP = newNode
End If
newNode.Data = GetData()
myNodes.Add(newNode)
End Sub
To test, use the following:
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim display As New System.Text.StringBuilder
For Each hold As Node In myNodes
If hold.NextP IsNot Nothing Then 'LastOn won't have a reference to append.
display.AppendLine(hold.Data & " ref: " & hold.NextP.Data)
Else
display.AppendLine(hold.Data & " ref: N/A")
End If
Next
MessageBox.Show(display.ToString)
End Sub
The difference is the code above will help you keep track of your order by using a collection. When a new item is added, the previous item's "NextP" is assigned to it.
-
Sep 29th, 2008, 11:26 AM
#10
Re: [2005] Manual pointers in VB
And just for your reference... Taking into account the same concept as above, but building on the object; you could do something like the following:
VB Code:
Private myNodes As Generic.SortedList(Of Int32, Node)
Public Class Node
Private m_data As String
Private m_node As Node
#Region "Constructors"
Public Sub New()
Me.New(String.Empty, Nothing)
End Sub
Public Sub New(ByVal data As String)
Me.New(data, Nothing)
End Sub
Public Sub New(ByVal node As Node)
Me.New(String.Empty, node)
End Sub
Public Sub New(ByVal data As String, ByVal node As Node)
m_data = data
m_node = node
m_currentInstance = Me
If node Is Nothing Then node = m_currentInstance _
Else node.Node = m_currentInstance
End Sub
#End Region
#Region "Members"
Public Property Data() As String
Get
Return m_data
End Get
Set(ByVal value As String)
m_data = value
End Set
End Property
Public Property Node() As Node
Get
If Not Object.ReferenceEquals(m_currentInstance, Me) Then Return m_node
Return Nothing
End Get
Set(ByVal value As Node)
m_node = value
End Set
End Property
#End Region
#Region "Singleton Members"
Private Shared m_currentInstance As Node
Protected Friend Shared ReadOnly Property CurrentInstance() As Node
Get
Return m_currentInstance
End Get
End Property
#End Region
End Class
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
myNodes = New Generic.SortedList(Of Int32, Node)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myNodes.Add(myNodes.Count, New Node(GetData, Node.CurrentInstance))
End Sub
Testing is more or less the same code:
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim display As New System.Text.StringBuilder
For Each hold As KeyValuePair(Of Int32, Node) In myNodes
If hold.Value.Node IsNot Nothing Then
display.AppendLine(hold.Value.Data & " ref: " & hold.Value.Node.Data)
Else
display.AppendLine(hold.Value.Data & " ref: N/A")
End If
Next
MessageBox.Show(display.ToString)
End Sub
I would deffinitely encourage you to look at both. Even if you're not completely comfortable with the above - ask questions and learn right. .Net and all that it encompasses is an OOP language. It's not until you learn how to work with, manipulate and enhance these objects that the framework starts to show its real potential.
-
Sep 29th, 2008, 11:43 AM
#11
Fanatic Member
Re: [2005] Manual pointers in VB
The whole point of VB is so you DON'T have to do this kind of stuff right?
I can understand in C++ you're teacher making you make this stuff and then telling you about the STL, but in VB? What's the point?
Unless you wanna make a custom one? But I thought Linked Lists were Linked Lists.
Last edited by Seraph; Sep 29th, 2008 at 11:47 AM.
-
Sep 29th, 2008, 01:44 PM
#12
Re: [2005] Manual pointers in VB
 Originally Posted by Seraph
The whole point of VB is so you DON'T have to do this kind of stuff right?
I can understand in C++ you're teacher making you make this stuff and then telling you about the STL, but in VB? What's the point?
Unless you wanna make a custom one? But I thought Linked Lists were Linked Lists.
The point of VB is for RAD-type projects. While historically, VB could have been referred to as a lazy professional's language - this has deffinitely not been the case with VB.Net. Can you slof off with VB.Net and only utilize what is immediately available? Yeah - very easily.
But keep in mind these two things:
A) You're working with a framework. Code isn't being rewritten for each application and those types of projects will start to become exceedingly rare. The IDE's functionality with design time drag/drop has become so easy to use - most companies/projects could teach people who are not IT savy to build their own layouts and basic functionality (say... the design department).
B) Your world has collided with people who don't use lazy languages. J#, C#, C++, the linux community via Mono - all of them are players and contributers now. While you may create your object or application with the idea "it's good enough for VB," these people not only live for the details... They revel in finding ways make eachother's code look sloppy, inadequate or slow.
As for the LinkedList being a LinkedList - yes; that's true. But when you get into professional n-tier applications; you'll hear alot about BLL. These are objects that describe a business model.
In a simple example... You have employees and they belong to a department. The department is an object that contains a collection with references to all of these employees. Now, say you want to find all of the Employees who are currently in the office for this department. You could take the collection, do a for loop and check the status of each employee... But every application and process that required this functionality would have to duplicate the code. If you had an object called Department that housed the collection - you could place the for/loop inside of it and all the developer requesting it would need to do is call the function that returned a filtered collection.
That demonstrates the convenience of this model, but there is also a practicality side to it. Say your boss finds out that all of these lowly employees know his status. This infuriates him and he only wants people who are employeeType 2 or greater to be able to see this information. Using just a LinkedList, each and every place people copied that for/loop would need to be updated. But if you had a department object in place providing a way to query on that precise data - you only need to modify it with this new business rule. The changes would scale out to all applications/processes and no more effort would be needed.
-
Sep 29th, 2008, 02:33 PM
#13
Fanatic Member
Re: [2005] Manual pointers in VB
I see. Thanks for taking the time to explain that.
I've been doing stuff like this since HighSchool, getting my start doing HTML at 13 and then PHP in HS then before doing VB6 in 12th grade for a class and then going beyond that on myown. Now I'm in college for programming and I may have taught myself some stuff over the past 10 or so years but I don't know much about the realities of the business world just yet.
And thanks for the wiki links too.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|