-1

I am trying to implement an interface and use its methods, but when I create a Star object and try and use methods, they don't print anything.

Fair warning, I have not been coding for months, and I'm very new to C#(about a week in) so this is horrendous.

Any help on any issues in this code would be greatly appreciated

The first part of my code:

    interface CelestialBody
    {
        string getName();
        string getType();
        List<string> getOrbitals();
        void addOrbitals(string Orbital);
    }
    enum CelestialBodyType { Star, Planet, Satellite }
    
    class Star : CelestialBody
    {
        private string _Name;
        private List<string> Planets;
        private readonly string Type = "Star";
        public string getType() { return Type; } // Planet, Sun, Satellite
       
        public string Name
        {
            set
            {
                _Name = value;
            }

            get
            {
                return _Name;
            }

        }
        public string getName() { return _Name; }
        public List<string> getOrbitals() { return Planets; }
        public void addOrbitals(string NewPlanet) { Planets.Add(NewPlanet); }



        public Star() : this("No Name") // Sets the default value of Name
        {
        }


        public Star(string Name) : this(Name, CelestialBodyType.Star) // Set the default value of Classification
        {
        }
        //Designated Constructor
        public Star(string Name, CelestialBodyType Star)
        {
            this.Name = Name;
        }

    }

    
}

The main part of my code

 class Program
    {
        static void Main(string[] args)
        {
            Star star = new Star("The Sun");

            Console.Write(star.getName() + star.getType());
        }
    }
}
Marcus
  • 1

1 Answers1

0

I assume you're using Visual Studio 2017 or .NET Framework instead of .NET Core - in which case the problem is that your program is exiting immediately after it writes to the console which will cause the console output window to disappear, hence why you don't see any output.

Change your program to pause before it exits using Console.ReadLine():

static void Main(string[] args)
{
    Star star = new Star("The Sun");

    Console.WriteLine( star.getName() + star.getType() );

    Console.WriteLine( "Press [Enter] to exit." ); 
    Console.ReadLine();
}

VS2019 and .NET Core projects now keep the console output window open after your process exits.

Dai
  • 141,631
  • 28
  • 261
  • 374