I am trying to teach myself C#. I recently completed this exercise. The code works the the way its suppose to. My question is how could I make this more efficient or is there bad programing practices? Thanks

using System;
class testcontact
{
    public static void Main()
{
    InputWrapper iw = new InputWrapper();
    string cmd;
    
    do
    {
        
         Console.WriteLine("Add");   // menu of options
         Console.WriteLine("Remove");
         Console.WriteLine("Forward");
         Console.WriteLine("Reverse");
         Console.WriteLine("Find");
         Console.WriteLine("Quit");
         
        cmd = iw.getString(">");    // user input from Input wrapper class
        
     
        
    switch (cmd)
    { 

    case ("add"):    // new contact
    Console.WriteLine("\nAdding new contact\n");
    break;
    
    case "remove":  // remove contact
    Console.WriteLine("\nRemoving contact\n");
    break;

    case "forward":   //sort forward
    Console.WriteLine("\nSorting contacts forward\n");
    break;

    case "reverse":   // reverse sort
    Console.WriteLine("\nRevers contact sort\n");
    break;

    case "find":   // find contact
    Console.WriteLine("\nFind contact\n");
    break;

    default:    // all invalid commands will go here
    Console.WriteLine("valid commands are \n add \n remove \n forward \n reverse \n find quit\n");
    break;
    }
    
    }
    while (cmd != "quit");

    }

}

Recommended Answers

All 3 Replies

I am trying to teach myself C#. I recently completed this exercise. The code works the the way its suppose to. My question is how could I make this more efficient or is there bad programing practices? Thanks

using System;
class testcontact
{
    public static void Main()
{
    InputWrapper iw = new InputWrapper();
    string cmd;
    
    do
    {
        
         Console.WriteLine("Add");   // menu of options
         Console.WriteLine("Remove");
         Console.WriteLine("Forward");
         Console.WriteLine("Reverse");
         Console.WriteLine("Find");
         Console.WriteLine("Quit");
         
        cmd = iw.getString(">");    // user input from Input wrapper class
        
     
        
    switch (cmd)
    { 

    case ("add"):    // new contact
    Console.WriteLine("\nAdding new contact\n");
    break;
    
    case "remove":  // remove contact
    Console.WriteLine("\nRemoving contact\n");
    break;

    case "forward":   //sort forward
    Console.WriteLine("\nSorting contacts forward\n");
    break;

    case "reverse":   // reverse sort
    Console.WriteLine("\nRevers contact sort\n");
    break;

    case "find":   // find contact
    Console.WriteLine("\nFind contact\n");
    break;

    default:    // all invalid commands will go here
    Console.WriteLine("valid commands are \n add \n remove \n forward \n reverse \n find quit\n");
    break;
    }
    
    }
    while (cmd != "quit");

    }

}

Given a first look, the only change I could suggest would be using an Enum rather than hard coding your values in the switch. an Enum is a collection of constant string values, that are usable in the cases of a switch statement. this way, you can refer to it with the Intellisense too.

The other thing I maybe would change would be using a Console.ReadLine() rather than an inputWrapper but that's cause it's the way I always worked so I don't know much about InputWrapper. But the Enum is the thing I'd consider the most in the suggestions

//EchoName.cs
using System;
class Echo
{
    public static void Main(string[] args)
    {
        InputWrapper iw = new InputWrapper();
    }
}

Why do i get an error with blue lining under that.

Please help

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.