Results 1 to 13 of 13

Thread: [2005] Manual pointers in VB

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    [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.

  2. #2

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  3. #3
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  5. #5
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    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:
    1. '   non mea culpa...
    2.     Public Sub DoNodes()
    3.         Dim n As New Nodesi
    4.         Dim nn As New Nodesi(n)
    5.         MsgBox(n.GetData & vbNewLine & nn.GetParent.GetData)
    6.     End Sub
    7.  
    8.     Public Class Nodesi
    9.         Private _parent As Nodesi
    10.  
    11.         Public Sub New()
    12.         End Sub
    13.         Public Sub New(ByVal parent As Nodesi)
    14.             _parent = parent
    15.         End Sub
    16.  
    17.         Public Function GetData() As String
    18.             Return CStr(Me.GetHashCode)
    19.         End Function
    20.  
    21.         Public Function GetParent() As Nodesi
    22.             If _parent Is Nothing Then Return Me Else Return _parent
    23.         End Function
    24.     End Class
    I've been known to be wrong ...

  6. #6
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    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:
    1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.         Dim tailObject, middleObject, leadObject As Nodei
    3.  
    4.         leadObject = New Nodei
    5.         leadObject.DataHolder = "Lead"
    6.  
    7.         middleObject = New Nodei
    8.         middleObject.DataHolder = "Middle"
    9.         middleObject.Follows = leadObject
    10.  
    11.         tailObject = New Nodei
    12.         tailObject.DataHolder = "Tail"
    13.         tailObject.Follows = middleObject
    14.     End Sub
    15.  
    16.     Public Class Nodei
    17.         Public DataHolder As String
    18.         Public Follows As Nodei
    19.     End Class

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] Manual pointers in VB

    Quote 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

  8. #8

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  9. #9
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [2005] Manual pointers in VB

    If you want, try this approach. It's a little more digestible for what you want.
    VB Code:
    1. Public Class Node
    2.         Public Data As String
    3.         Public NextP As Node
    4.     End Class
    5.     Private myNodes As Generic.List(Of Node)
    6.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    7.         myNodes = New Generic.List(Of Node)
    8.     End Sub
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Dim newNode As New Node
    11.         If myNodes.Count > 0 Then
    12.             myNodes(myNodes.Count - 1).NextP = newNode
    13.         End If
    14.         newNode.Data = GetData()
    15.         myNodes.Add(newNode)
    16.     End Sub

    To test, use the following:
    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim display As New System.Text.StringBuilder
    3.         For Each hold As Node In myNodes
    4.             If hold.NextP IsNot Nothing Then  'LastOn won't have a reference to append.
    5.                 display.AppendLine(hold.Data & " ref: " & hold.NextP.Data)
    6.             Else
    7.                 display.AppendLine(hold.Data & " ref: N/A")
    8.             End If
    9.         Next
    10.         MessageBox.Show(display.ToString)
    11.     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.

  10. #10
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    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:
    1. Private myNodes As Generic.SortedList(Of Int32, Node)
    2.     Public Class Node
    3.         Private m_data As String
    4.         Private m_node As Node
    5. #Region "Constructors"
    6.         Public Sub New()
    7.             Me.New(String.Empty, Nothing)
    8.         End Sub
    9.         Public Sub New(ByVal data As String)
    10.             Me.New(data, Nothing)
    11.         End Sub
    12.         Public Sub New(ByVal node As Node)
    13.             Me.New(String.Empty, node)
    14.         End Sub
    15.         Public Sub New(ByVal data As String, ByVal node As Node)
    16.             m_data = data
    17.             m_node = node
    18.  
    19.             m_currentInstance = Me
    20.             If node Is Nothing Then node = m_currentInstance _
    21.                 Else node.Node = m_currentInstance
    22.         End Sub
    23. #End Region
    24. #Region "Members"
    25.         Public Property Data() As String
    26.             Get
    27.                 Return m_data
    28.             End Get
    29.             Set(ByVal value As String)
    30.                 m_data = value
    31.             End Set
    32.         End Property
    33.         Public Property Node() As Node
    34.             Get
    35.                 If Not Object.ReferenceEquals(m_currentInstance, Me) Then Return m_node
    36.                 Return Nothing
    37.             End Get
    38.             Set(ByVal value As Node)
    39.                 m_node = value
    40.             End Set
    41.         End Property
    42. #End Region
    43. #Region "Singleton Members"
    44.         Private Shared m_currentInstance As Node
    45.         Protected Friend Shared ReadOnly Property CurrentInstance() As Node
    46.             Get
    47.                 Return m_currentInstance
    48.             End Get
    49.         End Property
    50. #End Region
    51.     End Class
    52.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    53.         myNodes = New Generic.SortedList(Of Int32, Node)
    54.     End Sub
    55.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    56.         myNodes.Add(myNodes.Count, New Node(GetData, Node.CurrentInstance))
    57.     End Sub
    Testing is more or less the same code:
    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim display As New System.Text.StringBuilder
    3.         For Each hold As KeyValuePair(Of Int32, Node) In myNodes
    4.             If hold.Value.Node IsNot Nothing Then
    5.                 display.AppendLine(hold.Value.Data & " ref: " & hold.Value.Node.Data)
    6.             Else
    7.                 display.AppendLine(hold.Value.Data & " ref: N/A")
    8.             End If
    9.         Next
    10.         MessageBox.Show(display.ToString)
    11.     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.

  11. #11
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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.

  12. #12
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [2005] Manual pointers in VB

    Quote 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.

  13. #13
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    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
  •  



Click Here to Expand Forum to Full Width