Results 1 to 5 of 5

Thread: How do I code a LinkedList in VB.Net?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    How do I code a LinkedList in VB.Net?

    Hello everybody,

    I am new to all things .Net, but have several years experience of OOP in Java from my college days.

    I have recently written a sliding block/tile type puzzle game in Java and I would now like to rewrite it in VB.Net. The programming construct I used to manage the logic behind the game was a LinkedList - each element of the list represented one block/tile.

    Basically speaking In Java I coded the following:
    Code:
    LinkedList tileManager = new LinkedList();
    Then after populating my list like so:
    Code:
    //The int[] tileLayout was used to hold the colour values
    //of the tiles - 0 for black, 1 for white.
    public void populateLinkedList(int[] tileLayout)
    {
    	
    	for(int i = 0; i < tileLayout.length; i++)
    	{
    			
    		tileManager.add((Integer)tileLayout[i]);
    			
    	}	
    				
    }
    I could move the tiles in the list in the following manner.
    Code:
    //Note that the boolean variable direction used below was initialised 
    //earlier in the code depending on the users actions.
    if(direction==true)
    {
    	//Move the tiles clockwise in the list.	
    	tileManager.addFirst(tileManager.removeLast());
    			
    }
    else if(direction==false)
    {
    	
    	//Move the tiles anti-clockwise in the list.	
    	tileManager.addLast(tileManager.removeFirst());
    			
    }
    How then would I go about coding the same functionality in VB.Net?

    Thank you for your help.

    Regards,

    The Thing.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I code a LinkedList in VB.Net?

    .NET 2.0 has a LinkedList class. That code would be almost exactly the same in C# as it is in Java. There are links to C# -> VB.NET converters in my signature. Have a ball.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: How do I code a LinkedList in VB.Net?

    I mean, you could right your own really easily. On the fly untested something like:

    VB Code:
    1. Public Class clsTile
    2.     Public value As Int16
    3.     Public NextT As clsTile
    4.     Public PrevT As clsTile
    5. End Class
    6.  
    7. Public Class MyLinkedList
    8.     Private First As clsTile
    9.     Private Last As clsTile
    10.  
    11.     Public Sub AddFirst(ByVal oTile As clsTile)
    12.  
    13.         If First Is Nothing Then
    14.             oTile = First
    15.             oTile = Last
    16.             oTile.NextT = Nothing
    17.             oTile.PrevT = Nothing
    18.         Else
    19.             First.PrevT = oTile
    20.             oTile.NextT = First
    21.             oTile.PrevT = Nothing
    22.             First = oTile
    23.         End If
    24.     End Sub
    25.  
    26.     Public Sub AddLast(ByVal oTile As clsTile)
    27.         If Last Is Nothing Then
    28.             oTile = First
    29.             oTile = Last
    30.             oTile.NextT = Nothing
    31.             oTile.PrevT = Nothing
    32.         Else
    33.             Last.NextT = oTile
    34.             oTile.PrevT = Last
    35.             oTile.NextT = Nothing
    36.             Last = oTile
    37.         End If
    38.     End Sub
    39.  
    40.     Public Function RemoveFirst(ByVal oTile As clsTile) As clsTile
    41.         Dim oReturn As clsTile
    42.  
    43.         oReturn = First
    44.  
    45.         If oReturn.NextT Is Nothing = False Then
    46.             First = oReturn.NextT
    47.             oReturn.PrevT = Nothing
    48.         Else
    49.             First = Nothing
    50.             Last = Nothing
    51.         End If
    52.  
    53.         oReturn.NextT = Nothing
    54.         oReturn.PrevT = Nothing
    55.  
    56.         Return oReturn
    57.     End Function
    58.  
    59.     Public Function RemoveLast(ByVal oTile As clsTile) As clsTile
    60.         Dim oReturn As clsTile
    61.  
    62.         oReturn = Last
    63.  
    64.         If oReturn.PrevT Is Nothing = False Then
    65.             Last = oReturn.PrevT
    66.             oReturn.NextT = Nothing
    67.         Else
    68.             First = Nothing
    69.             Last = Nothing
    70.         End If
    71.  
    72.         oReturn.NextT = Nothing
    73.         oReturn.PrevT = Nothing
    74.  
    75.         Return oReturn
    76.     End Function
    77. End Class
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Re: How do I code a LinkedList in VB.Net?

    Hi folks, thanks for your help.

    Really the only reason I used a linked list was for convenience - the ease in which I could shift the tiles left or right in the list using a single line of code:

    Code:
    tileManager.addFirst(tileManager.removeLast());
    I had two classes - one to handle the display, and another to handle the logic behind the game. The class handling the display used an int[] to store the colour values (0 or 1) of the tiles so perhaps a simple sub-routine/function which takes, manipulates, and returns an int[] is the way to go.

    Thanks again.

    The Thing.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

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

    Re: How do I code a LinkedList in VB.Net?

    I would stick with the class concept, though you might alter the functionality somewhat. Returning an array would be possible, but it seems like an ugly solution. Better to have the array wrapped in a class, with the function as a memeber that manipulates it.
    My usual boring signature: Nothing

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