Skip to content

This is a program where are using Indexer Methods and Exception Handlings.

Notifications You must be signed in to change notification settings

SatSargsyan/SolarSystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Solar System

Declaration of an indexer is to some extent similar to a property. The difference is that indexer accessors require an index.

Like a property, you use get and set accessors for defining an indexer. However, where properties return or set a specific data member, indexers return or set a particular value from the object instance.

ndexers are defined with the this keyword.

For example:

class Clients {
  private string[] names = new string[10];

  public string this[int index] {
    get {
      return names[index];
    }
    set {
      names[index] = value;
    }
  }
}

The indexer definition includes the this keyword and an index, which is used to get and set the appropriate value.

Now, when we declare an object of class Clients, we use an index to refer to specific objects like the elements of an array:

Clients c = new Clients();
c[0] = "Dave";
c[1] = "Bob";

Console.WriteLine(c[1]);
//Outputs "Bob"  

In this program are using Indexer methods, Exception handlings and Extension Methods.

namespace Solar_System
{
    class SolarSystem
    {
        string[] planets = { "The Sun", "Mercury", "Venus", "Earth",
            "Mars", "Jupiter", "Saturn","Uranus", "Neptune", "Pluto" };
        private int GetPlanet(string testPlanet)
        {
            for (int j = 0; j < planets.Length; j++)
            {
                if (planets[j] == testPlanet)
                {
                    return j;
                }
            }
            throw new ArgumentOutOfRangeException
                (testPlanet, "testPlanet must be correct name of the planet");
        }
        public int this[string planet]
        {
            get
            {
                return GetPlanet(planet);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            SolarSystem plan = new SolarSystem();
            Console.WriteLine("Please enter name of planet to know its number from the set \"The Sun, Mercury, Venus, Earth,Mars, Jupiter, Saturn, Uranus, Neptune, Pluto\" ");
            string st =Console.ReadLine();
            st = st.UppercaseFirstLetter();
            Console.WriteLine(plan[st]);

            //Console.WriteLine(plan["Marsss"]);

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

solar-system

When we are entering the correct name of the planet, the program is returning its number of Galaxy.

And if the name of the planet is in the incorrect form, there is happening exception.
For example Marssss.

sol sys3

About

This is a program where are using Indexer Methods and Exception Handlings.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages