0

I'm learning C#, I have this code :

namespace foo
{
    public class Personnes
    {
        string[] m_Noms;
        int m_NbElt;
        int m_Max;

        public Personnes(int Max)
        {
            m_Max = Max;
            m_NbElt = 0;
            m_Noms = new string[Max];
        }

        public int this[string Nom]
        {
            get { return Array.IndexOf(m_Noms, Nom); }
        }

        public string this[int i]
        {
            get { return m_Noms[i]; }
            set { m_Noms[i] = value;m_NbElt++; }
        }
    }
    class Prog
    {
        static void Main(string[] args)
        {
            Personnes Tableau = new Personnes(4);
            Tableau[0] = "Anna";
            Tableau[1] = "Ingrid";
            Tableau[2] = "Maria";
            Tableau[3] = "Ulrika";
            Console.WriteLine(Tableau[1]); 
            Console.WriteLine(Tableau["Maria"]);
            Console.WriteLine(Tableau[10]); 
            Console.WriteLine(Tableau["Toto"]); 


        }
    }
}

I've been told that Console.WriteLine(Tableau[10]); should display null and the next line -1 but it doesn't, instead I have an error IndexOutOfRange, why ?

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
rn605435
  • 175
  • 2
  • 12

3 Answers3

4

See you have initialized your array Tableau with just 4 Personnes(4). And you are trying to get what is at Tableau[10], so your are correctly getting IndexOutOfRange exception. The index that you are seeking is out of the range specified.

Yogi
  • 9,174
  • 2
  • 46
  • 61
  • OP has initialised the array to 4 not 5 using Personnes(4) - Tableau[4] would still generate an IndexOutOfRange exception. – PaulF Sep 20 '18 at 10:27
  • _"m_Noms = new string[Max];"_ creates an array of length 4 (0-3) - it does not set the upper bound to 4. Try it & see. – PaulF Sep 20 '18 at 10:32
  • @PaulF - Oh yes, you are right. Edited my ans. Thanks – Yogi Sep 20 '18 at 10:33
  • 1
    You need to edit the last bit as well - all strings are allocated & none would return null. – PaulF Sep 20 '18 at 10:34
4

It is displaying IndexOutOfRangeException because you have set Tableau to have only 4 strings and any array retrieval beyond the index range[0 to 3] will result in this.

public string this[int i]
    {
        get { return m_Noms[i]; } <--  displays error if outside the range
        set { m_Noms[i] = value;m_NbElt++; }
    }

If you have to display null, then you need to add conditions in the indexer logic to check for the index value and if it is out of range return null

user3785553
  • 121
  • 6
  • Ok I understand now, thank you! I've added condition on get if i > m_Max then return null and it works – rn605435 Sep 20 '18 at 10:49
  • 1
    @rn605435 : To be on the safe side it might be worth making that (i > m_Max) || (I < 0). – PaulF Sep 20 '18 at 13:39
1

I've been told that Console.WriteLine(Tableau[10]); should display null and the next line -1 but it doesn't, instead I have an error IndexOutOfRange, why ?

Because whoever told you that was wrong. Accessing an array with an index that does not exist should throw an exception.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • You were right thank you, but the explainations of the other answer allowed me to display the results I wanted – rn605435 Sep 20 '18 at 10:50