-4

I have a problem when trying to use variables inside a switch. They are variables that are assigned "outside" (the switch) and when I pass them as parameters it throws the folling error "Use of an unassigned variable""

    DateTime startDate, endDate;
                switch (int.Parse(cmbRepor.SelectedValue.ToString()))
                {
                    case 1:
                        startDate = new DateTime(anio, 1, 1);
                        endDate = new DateTime(anio, 3, 31);
                        break;
                    case 2:           
                        startDate = new DateTime(anio, 4, 1);
                        endDate = new DateTime(anio, 6, 30);
                        break;
                    case 3:
                        startDate = new DateTime(anio, 7, 1);
                        endDate = new DateTime(anio, 9, 30);
                        break;
                    case 4:
                        startDate = new DateTime(anio, 10, 1);
                        endDate = new DateTime(anio, 12, 31);
                        break;
                    default:
                        break;
                }

        ReporteLogica logica = new ReporteLogica();

            switch (int.Parse(cmbRepor.SelectedValue.ToString()))
            {
                case 1:
                    dgvListado.DataSource = logica.GetReporteClientesCuentasInhabilitadas(startDate, endDate);
                    break;
                case 2:
                    dgvListado.DataSource = logica.GetReporteClientesCuentasInhabilitadas(startDate, endDate);
                    break;
                case 3:
                    dgvListado.DataSource = logica.GetReporteClientesCuentasInhabilitadas(startDate, endDate);
                    break;
                case 4:
                    dgvListado.DataSource = logica.GetReporteClientesCuentasInhabilitadas(startDate, endDate);
                    break;
                default:
                    break;
            }
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
BrunoD
  • 9
  • 1
  • 1
    @MauricioGracia - translating it isn't the only problem, I'd imagine the answers would need translating too – Sayse Jun 25 '15 at 15:13
  • @Sayse willing to interact with the OP and translate the given answers as well – Mauricio Gracia Gutierrez Jun 25 '15 at 15:14
  • 1
    @MauricioGracia - Thats a lot of effort on your part but go ahead. Aside from that, this error is one that I'd imagine is easily google'd in any language and doesn't show research effort. – Sayse Jun 25 '15 at 15:14
  • @Sayse when you google for technical errors in spanish you get almost nothing or nothing good anyway – Mauricio Gracia Gutierrez Jun 25 '15 at 15:16
  • 1
    @MauricioGracia - I was curious, the second link for "Uso de variable local no asignada" in google is an [msdn page about that error](https://msdn.microsoft.com/es-es/library/4y7h161d.aspx) – Sayse Jun 25 '15 at 15:19
  • @Sayse msdn pages for compiler errors are so generic they usually dont provide any specific clues in how to solve it – Mauricio Gracia Gutierrez Jun 25 '15 at 15:23

1 Answers1

1

The problem is that you do not assign a value in the default-case of your switch. So what should the value be if none of the conditions fit? It stays uninitialized, the compiler does not know if this is even possible due to any restrictions that you made within your program during runtime. Assign a defualt-value for your dates:

DateTime startDate = DateTime.Now, endDate = DateTime.Now;
Cœur
  • 37,241
  • 25
  • 195
  • 267
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111