Code - Investing - FI

Visual Basic/C# Exercise 4 – Solution

Here is the solution for the exercise 4 in Visual Basic and C#. You will see minor differences but both languages are essentially the same.

Visual Basic

Module Program
    Sub Main()
        '1. Create the Dictionary
        Dim Positions = New Dictionary(Of Integer, String)

        '2. Add values
        Positions.Add(1, "Player 5")
        Positions.Add(2, "Player 1")
        Positions.Add(3, "Player 3")
        Positions.Add(4, "Player 2")
        Positions.Add(5, "Player 4")

        '3. Display the number of racers
        Console.WriteLine("There are {0} positions in the grid.", Positions.Count)

        '4. Replace the player at position 5 with the one at position 1
        Positions.Remove(1)
        Positions.Remove(5)

        Positions.Add(1, "Player 4")
        Positions.Add(5, "Player 5")

        '5. Display all players
        For Index As Integer = 1 To Positions.Count Step 1
            Console.WriteLine("{0} arrived at position {1}.", Positions(Index), Index)
        Next

        '6. Clear the Dictionary
        Positions.Clear()
        Console.ReadKey()
    End Sub
End Module

C#

using System;
using System.Collections.Generic;

namespace Exercise4CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            //1. Create the Dictionary
            Dictionary Positions = new Dictionary();

            //2. Add values
            Positions.Add(1, "Player 5");
            Positions.Add(2, "Player 1");
            Positions.Add(3, "Player 3");
            Positions.Add(4, "Player 2");
            Positions.Add(5, "Player 4");

            //3. Display the number of racers
            Console.WriteLine("There are {0} positions in the grid.", Positions.Count);

            //4. Replace the player at position 5 with the one at position 1
            Positions.Remove(1);
            Positions.Remove(5);

            Positions.Add(1, "Player 4");
            Positions.Add(5, "Player 5");

            //5. Display all players
            for (int Index = 1; Index <= Positions.Count; Index++)
                Console.WriteLine("{0} arrived at position {1}.", Positions[Index], Index);

            //6. Clear the Dictionary
            Positions.Clear();
            Console.ReadKey();
        }
    }
}

Remarks

For the number 4, I decided to remove the elements and insert them back. I could have Get both and then stored them in variables and finally reinsert them at their new position. Your call here, as long as the end result is the same!

I used a For loop for the number 5 of this exercise, I made this choice because I know how many loops I will have to make before all the elements are displayed, the Count parameter came handy.

Other than that I don’t think there are any other difficulties in this problem, if you wish to ask a question about this exercise, feel free to post it in the comments! Don’t give up!

Next article Visual Basic/C# Tutorial 16 - Object Oriented Programming (Classes)
Previous article Visual Basic/C# Exercise 4 - Collections.Generic

Related posts