Los indizadores no deben ser multidimensionales

Actualización: noviembre 2007

Nombre de tipo

IndexersShouldNotBeMultidimensional

Identificador de comprobación

CA1023

Categoría

Microsoft.Design

Cambio problemático

Motivo

Un tipo público o protegido contiene un indizador público o protegido que utiliza más de un índice.

Descripción de la regla

Los indizadores, es decir, las propiedades indizadas, deben utilizar un índice único. Los indizadores multidimensionales pueden reducir de forma significativa la utilidad de la biblioteca. Si el diseño requiere varios índices, reconsidere si un tipo representa un almacén de datos lógico. Si no es así, use un método.

Cómo corregir infracciones

Para corregir una infracción de esta regla, cambie el diseño para utilizar un solo entero o un índice de cadena o utilice un método en lugar del indizador.

Cuándo suprimir advertencias

Sólo suprima con cuidado una advertencia de esta regla después de tener en cuenta la necesidad del indizador no estándar.

Ejemplo

El ejemplo siguiente muestra un tipo, DayOfWeek03, con un indizador multidimensional que infringe la regla. El indizador se puede considerar un tipo de conversión y, por tanto, es más apropiado exponerlo como un método. El tipo se vuelve a diseñar en RedesignedDayOfWeek03 para cumplir la regla.

Imports System

Namespace DesignLibrary

    Public Class DayOfWeek03

        Private dayOfWeek(,) As String = {{"Wed", "Thu", "..."}, _
                                          {"Sat", "Sun", "..."}}
                                          ' ...

        Default ReadOnly Property Item(month As Integer, day As Integer) As String
            Get
                Return dayOfWeek(month - 1, day - 1)
            End Get
        End Property

    End Class

    Public Class RedesignedDayOfWeek03

        Private dayOfWeek() As String = _
            {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"}
        Private daysInPreviousMonth() As Integer = _
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30}

        Function GetDayOfWeek(month As Integer, day As Integer) As String
            Return dayOfWeek((daysInPreviousMonth(month - 1) + day) Mod 7)
        End Function

    End Class

End Namespace
using System;

namespace DesignLibrary
{
    public class DayOfWeek03
    {
        string[,] dayOfWeek = {{"Wed", "Thu", "..."}, 
                               {"Sat", "Sun", "..."}};
                               // ...

        public string this[int month, int day]
        {
            get
            {
                return dayOfWeek[month - 1, day - 1];
            }
        }
    }

    public class RedesignedDayOfWeek03
    {
        string[] dayOfWeek = 
            {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"};

        int[] daysInPreviousMonth = 
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};

        public string GetDayOfWeek(int month, int day)
        {
            return dayOfWeek[(daysInPreviousMonth[month - 1] + day) % 7];
        }
    }
}
using namespace System;

namespace DesignLibrary
{
    public ref class DayOfWeek03
    {
        array<String^, 2>^ dayOfWeek;

    public:
        property String^ default[int,  int]
        {
            String^ get(int month, int day)
            {
                return dayOfWeek[month - 1, day - 1];
            }
        }

        DayOfWeek03()
        {
            dayOfWeek = gcnew array<String^, 2>(12, 7);
            dayOfWeek[0,0] = "Wed";
            dayOfWeek[0,1] = "Thu";
            // ...
            dayOfWeek[1,0] = "Sat";
            dayOfWeek[1,1] = "Sun";
            // ...
        }
    };

    public ref class RedesignedDayOfWeek03
    {
        static array<String^>^ dayOfWeek = 
            {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"};

        static array<int>^ daysInPreviousMonth = 
            {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};

    public:
        String^ GetDayOfWeek(int month, int day)
        {
            return dayOfWeek[(daysInPreviousMonth[month - 1] + day) % 7];
        }
    };
}

Reglas relacionadas

Utilizar argumento integral o de cadena para los indizadores

Utilizar las propiedades donde corresponda