Separate the components of a date into different variables

-1
using System;
public class Videoclub{
    struct fecha{
        public string dia;
        public string mes;
        public string anyo;
    }
    struct pelicula{
        public string titulo;
        public string director;
        public string estreno;
        public fecha prestamo;
        public fecha devolucion;
    }
    public static void Main(){
        pelicula[] numPelicula = new pelicula[1000];
        string opcion;
        DateTime thisDay = DateTime.Today;
        int ultPosVacia = 0;
        string prestamo = "";
        string devolucion = "";
        string mess = "";
        bool salir = false;

        do{

            Console.WriteLine(thisDay.ToString("D"));
            Console.WriteLine("Que quieres hacer");
            Console.WriteLine("Opcion 1 - Insertar nueva pelicula al final");
            Console.WriteLine("Opcion 2 - Borrar pelicula");
            Console.WriteLine("Opcion 3 - Cerrar el programa");

            opcion = Console.ReadLine();

            switch (opcion){
                case "1":
                    Console.Write("Titulo de la pelicula? ");
                        numPelicula[ultPosVacia].titulo = Console.ReadLine();

                    Console.Write("Director de la pelicula? ");
                        numPelicula[ultPosVacia].director = Console.ReadLine();

                    Console.Write("Año de estreno de la pelicula? ");
                        numPelicula[ultPosVacia].estreno = Console.ReadLine();

                    Console.Write("Fecha de prestamo de la pelicula (dd/mm/aaaa)? ");
                        prestamo = Console.ReadLine();
                        numPelicula[ultPosVacia].prestamo.dia = prestamo.Remove(3,8);
                        mess = prestamo.Remove(0,3);
                        numPelicula[ultPosVacia].prestamo.mes = prestamo.Remove(3,5);
                        numPelicula[ultPosVacia].prestamo.anyo = prestamo.Remove(0,6);
                    Console.Write("Fecha de devolucion de la pelicula (dd/mm/aaaa)? ");
                        devolucion = Console.ReadLine();
                        numPelicula[ultPosVacia].devolucion.dia = devolucion.Remove(3,8);
                        mess = devolucion.Remove(0,3);
                        numPelicula[ultPosVacia].devolucion.mes = devolucion.Remove(3,5);
                        numPelicula[ultPosVacia].devolucion.anyo = devolucion.Remove(0,6);

                    ultPosVacia++;
                    Console.ReadKey();

                    break;
                case "2":
                    break;
                case "3":
                    salir = true;
                    break;
                default:
                    Console.WriteLine("*******************************************");
                    break;
            }


        }while(salir==false);

        Console.Clear();
    }
}

CMD error:

  

Unhandled exception: System.ArgumentOutOfRangeException: The index and the count must refer to a location in the chain. Parameter name: count in System.String.Remove (Int32 startIndex, Int32 count) in Videoclub.Main ()

    
asked by jesusAG28 06.11.2017 в 20:10
source

2 answers

1

Your error must come from the following statement:

numPelicula[ultPosVacia].prestamo.dia = prestamo.Remove(3,8);

When I asked you what you thought this sentence was, you answered me:

  

in case you enter 04/22/2015: delete from position 3 (the first bar), 8 characters.

But that is not the case. The positions (or indexes) start with 0 , not 1 . Therefore, when you indicate the index 3 , in reality this refers to the fourth character, not the third. And of course, since there are no 8 characters from the fourth, this gives you an error.

That's why, in this case, the correct expression was:

prestamo.Remove(2,8);

or, even simpler:

prestamo.Remove(2);

... where you omit the second parameter, which means that you will erase all the characters from the specified index.

I warn you in advance that, by correcting this error, you will realize that you have other errors. If you take the time to debug the code, you will surely find how to resolve other errors.

Personally, for this type of operation, I find it easier to use String.Substring than String.Remove , if you want to take a look.

Or, even better, when it comes to dates, instead of trying to extract the elements of the date by hand, it is better to use existing classes that already have this integrated logic. For example, this is another simpler way to extract the information you are looking for using DateTime.ParseExact() :

using System;
using System.Globalization;

// ...

string prestamo = @"22/04/2015";
DateTime fecha = DateTime.ParseExact(prestamo, "dd/MM/yyyy", CultureInfo.InvariantCulture);
int dia = fecha.Day;
int mes = fecha.Month;
int year = fecha.Year;
    
answered by 06.11.2017 в 21:14
1

If you only receive the date with separator bars, what you can do is a split .

For example, if the date comes like this:

var VariableDondeRecibesLaFecha = "06/11/2017";

string[] separarFecha= VariableDondeRecibesLaFecha.Split('/');
string dia = separarFecha[0];
string mes = separarFecha[1];
string anio= separarFecha[2];

The result would be:

  

dia = "06";

     

mes = "11";

     

anio = "2017";

It would be another option to consider.

    
answered by 06.11.2017 в 23:00