-1

I'm a novice programmer. I've been writing scripts for about 9 to 11 weeks. Any how I have been buying books per the posting that alot of you guys have recommended. I'm trying to learn "OOP", and it can be little challenging. I have worked the exercises in back of the book. The first exercise told me to make a Console Application program that prompts the user for a type of sport, and the coaches name.

  1. My first question is, did I do this question right?
  2. Are there other ways of doing the same thing, but in different process?
  3. I do realize that I do not have any "try/catches", the exercise told me not to add to this exercise problem. So we will worry about this later.

My code:

static void Main(string[] args)
{
    Sport sport01 = new Sport();
    sport01.TypeOfSport = GetSportsName();
    sport01.CoachesName = GetCoachesName();
    Console.WriteLine();
    Console.WriteLine(sport01.ToString());
    System.Threading.Thread.Sleep(8000);
}
// Prompting user input for type of sport.
public static string GetSportsName()
{
    string typeOfSport;
    Console.Write("Enter Sports Name : ");
    typeOfSport = Console.ReadLine();
    return typeOfSport;
}
// Prompting user inout for coaches name.
public static string GetCoachesName()
{
    string coachesName;
    Console.Write("Enter Coaches Name : ");
    coachesName = Console.ReadLine();
    return coachesName;
}

class Sport
{
    protected string typeOfSport;
    protected string coachesName;      
    public Sport()
    {
        typeOfSport = "Not Given";
        coachesName = "Not Given";
    }
    public Sport(string typeOS, string coachesN)
    {
        typeOfSport = typeOS;
        coachesName = coachesN;
    }
    public string TypeOfSport
    {
        get { return typeOfSport; }
        set { typeOfSport = value; }
    }
    public string CoachesName
    {
        get { return coachesName; }
        set { coachesName = value; }
    }
    public override string ToString()
    {
        return "\n" + "\n" +
           "\nSports Name : " + typeOfSport +
           "\n" +
           "\nCoaches Name : " + coachesName;
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 1
    Consider asking this question on the [Code Review Stack Exchange](https://codereview.stackexchange.com/). – John Wu Jun 09 '18 at 01:09
  • It's not clear how we can help you at this point. I would suggest you not to invest too much thought in OOP now and learn by doing c#. One thing that stands out as wrong is the instantiation of `sport01` *before* getting the data from the user, and you don't even assign the gathered data afterwards – Camilo Terevinto Jun 09 '18 at 01:09
  • Mr. Terevinto, That is very good information. I will look into that. Thank you for the suggestion. – solidworksToCSharp Jun 10 '18 at 14:35

1 Answers1

0

It is sometimes better to treat a class as immutable; once created, it never changes. For example:

class Sport
{
    protected readonly string typeOfSport;
    protected readonly string coachesName;      

    public Sport(string typeOfSport, string coachesName)
    {
        this.typeOfSport = typeOfSport;
        this.coachesName = coachesName;
    }
    public string TypeOfSport
    {
        get { return typeOfSport; }
    }
    public string CoachesName
    {
        get { return coachesName; }
    }
    public override string ToString()
    {
        return "\n" + "\n" +
           "\nSports Name : " + typeOfSport +
           "\n" +
           "\nCoaches Name : " + coachesName;
    }
}

Now you don't have to deal with a Sport that might not have a coach's name or type, and you can get rid of those ugly "Not Given" values:

static void Main(string[] args)
{
    var typeOfSport = GetSportsName();
    var coachesName = GetCoachesName();
    var sport = new Sport(typeOfSport, coachesName);
    Console.WriteLine();
    Console.WriteLine(sport.ToString());
    System.Threading.Thread.Sleep(8000);
}

Notice at no time does a Sport exist that is not valid.

This approach does create more temporary variables, but that is not necessarily a bad thing, and in trade you never have to worry about validating a Sport before using it.

If you do want to keep the class as mutable, as in your original code, I'd advise against using a magic number (i.e. "Not Given") as these are considered a code smell. It would break, for example, if someone ever named their child "Not Given" and he grew up to be a coach. To represent a missing value, the convention is to simply use null.

Also, in general it is advisable to declare a variable the first time you use it, like this:

public static string GetSportsName()
{
    Console.Write("Enter Sports Name : ");
    var typeOfSport = Console.ReadLine();
    return typeOfSport;
}

You'll also notice I changed most of the variable declarations to use var, which allows the compiler to infer the type from the value being assigned.

As a final note, it is poor form to name a variable with a number (e.g. sport01). If you have more than one, put them in an array or List. If you don't have more than one, don't just put an 01 on there, as developers may expect that there is an 02 or 03 somewhere. If you do need two instances that don't logically belong in a list together, give them names describing their purpose, e.g. morningSport and eveningSport or something.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Shouldn't a getter-only auto-property make more sense at this point? `public string TypeOfSport { get; }` No need for the explicitly defined backing field – Camilo Terevinto Jun 09 '18 at 01:18
  • Sure, you could do that starting with C#6. As a pedagogical example I chose to keep more consistent with the original and keep the `readonly` explicit. – John Wu Jun 09 '18 at 01:21
  • Mr. Wu, Thank You very much for this information. I should learn more about this in my C.S. class. I have taken my first "Foundations of Computer Science" class about six or seven weeks ago. I will learn more about about OOP in about 10 weeks when the Fall 2018 semester starts. Thank you again, I know what questions to ask when the semester starts. – solidworksToCSharp Jun 10 '18 at 14:44