I am trying to teach myself C#. I spent about ten hours trying to find a solution and could not come up with a working one. So I am trying this from a different approach. I got the answer and I am going to work my way back to the start. I have added comment to show my line of thinking.

// TestContacts.cs

using System;

public class TestContacts
{
	public static void Main()
	{
		string[] names = new string[10];
		int count = 0;  // what is var for? My first thought was it placed the new entry in the next available spot but should that happen anyway?
		int index;
		string target;
		names[count++] = "Tom";
		names[count++] = "Dick";
		names[count++] = "Harry";
		InputWrapper iw = new InputWrapper();
		Console.WriteLine("Enter command, quit to exit");
		string cmd = iw.getString("> ");
		while (! cmd.Equals("quit"))
		{
			
			switch (cmd)
			{
				case "add":
					
					string name = iw.getString("name: ");
					names[count++] = name; //This why I think count determines place in the array
					break;
				case "forward":
				
					for (int i = 0; i < count; i++)  // is this i only avaiable in this case? I think it has to be 
						Console.WriteLine(names[i]);
					break;
				case "reverse":
					
					for (int i = count - 1; i >= 0; i--)
						Console.WriteLine(names[i]);
					break;
				case "find":
				
					target = iw.getString("target name: "); 
					index = Search(names, count, target); // Is this a call to the search method at the bottom?
					if (index != -1)
						Console.WriteLine("{0} found at {1}", target, index);
					else
						Console.WriteLine("{0} not found", target);
					break;
				case "remove":
					
					target = iw.getString("remove name: ");
					index = Search(names, count, target);
					if (index != -1)
					{
						// Move names to fill hole
						for (int i = index; i < count - 1; i++)
							names[i] = names[i + 1];
						count--;
						Console.WriteLine("{0} has been removed", target);
					}
					else
						Console.WriteLine("{0} not found", target);
					break;
				default:
					Console.WriteLine("The following commands are available:");
					Console.WriteLine("   add       add a contact");
					Console.WriteLine("   forward   show contacts in forward order");
					Console.WriteLine("   reverse   show contacts in reverse order");
					Console.WriteLine("   find      find a contact");
					Console.WriteLine("   remove    remove a contact");
					Console.WriteLine("   quit      exit the program");
					break;
			}
			cmd = iw.getString("> ");
		}
	}
	public static int Search(string[] array, int count, string target)
	{
		int i = 0;
		bool found = false;
		while (!found && i < count)
		{
			if (target == array[i])
				found = true;
			else
				i++;
		}
		if (found)
			return i;
		else
			return -1;
	}
}

First, you are correct in thinking that the count is used for inserting the next record. I would advise you look into the List<T> datatype though. Your code uses an array of strings and is initialised with a size of 10: string[] names = new string[10]; This means you can only ever hold 10 names and if you add an 11th you throw an IndexOutOfBoundsException because you are trying to write to a part of the array that doesnt exist. A List is a collection that resizes itself each time you add/remove an item so you dont have to explicitly shuffle the items down when you delete one etc. They are much more versatile so I would recommend you take the time to learn how they work.

The For loop is used to iterate through code a preset number of times. You declare a counter (typically named i, j, k, etc) that tracks how many times you have gone through the loop.
The counter only exists within the loop.
In C#, code blocks are defined using bracers: {}.
A for loop that executes a block of code is written as:

for (int i = 0; i<10; i++)
{
    //i exists within these bracers
    dosomething;
    dosomethingelse;
}

If you only want to run a single line you optionally dont need to define the block so you can leave out the bracers, but i still only exists in the context of that single line 'inside' the loop.

You are correct, that is a call to the search method. Methods are always followed by a pair of parentheses: (), inside which you place the variables to be passed to the method. Some methods dont require any input parameters so they have empty parentheses.
The search method is declared with with a return type of int: public static int Search(string[] array, int count, string target) and accepts a string array, an int and a string as input paramters.
If a method has a return type then it has to return a value of that type, in this case it returns the index of the contact, or -1 if the contact isnt found. In your code, the return value is assigned to the variable 'index'.

Lastly, your assumption is correct; when an item is deleted the items in the array have to be shuffled down to fill the now empty space.

Phew...Hope this has helped :)

commented: Thanks for the help +0

Yes that was very helpful thanks for taking the time to help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.