Error ArrayList

0

I have the following code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQArrayListTips05
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList lista = new ArrayList();
            lista.AddRange(new object[] { "hola", 5, 6.7, false, 4, 2, "saludos", 3.5, 3 });

            var enteros = lista.OfType<int>();

            foreach (int n in enteros)
                Console.WriteLine(n);
            Console.WriteLine("---------------");

            ArrayList estudiantes = new ArrayList();

            {
                new CEstudiante("Jhon", "A203", "Telemática", 3.6),
                new CEstudiante("Ana", "B304", "Informática", 4.2),
                new CEstudiante("Pedro", "C405", "Psicología", 2.6),
                new CEstudiante("Erick", "D506", "Escritura", 4.9),
                new CEstudiante("Jaime", "E607", "Ingles", 1.6),
                new CEstudiante("Juan", "F708", "Humanidades", 3.8)
            };

            var estL = estudiantes.OfType<CEstudiante>();

            var reprobados = from e in estL
                             where e.Promedio <= 3.0
                             select e;

            Console.WriteLine("Reprobados");
            foreach (CEstudiante r in reprobados)
                Console.WriteLine(r);
            Console.WriteLine("Presione un tecla para continuar");
            Console.ReadKey();
        }
    }
}

But when I run it, I get the following error:

  

Severity Code Description Project File Line Status suppressed   Error CS1002 Expected;

By setting the ; where the compiler asks for it, the program does not work as it should. Does anyone know why this happens?

    
asked by Jhon Bohorquez 29.11.2018 в 15:18
source

2 answers

1

You must replace this part of the code:

ArrayList estudiantes = new ArrayList()
{
    new CEstudiante("Jhon", "A203", "Telemática", 3.6),
    new CEstudiante("Ana", "B304", "Informática", 4.2),
    new CEstudiante("Pedro", "C405", "Psicología", 2.6),
    new CEstudiante("Erick", "D506", "Escritura", 4.9),
    new CEstudiante("Jaime", "E607", "Ingles", 1.6),
    new CEstudiante("Juan", "F708", "Humanidades", 3.8)
};

You have a semicolon where it is not

    
answered by 29.11.2018 / 15:27
source
3

Several things:

  • As you are told, the first thing is to remove the ; of this line ArrayList estudiantes = new ArrayList();

       ArrayList estudiantes = new ArrayList(){
            new CEstudiante("Jhon", "A203", "Telemática", 3.6),
            new CEstudiante("Ana", "B304", "Informática", 4.2),
            new CEstudiante("Pedro", "C405", "Psicología", 2.6),
            new CEstudiante("Erick", "D506", "Escritura", 4.9),
            new CEstudiante("Jaime", "E607", "Ingles", 1.6),
            new CEstudiante("Juan", "F708", "Humanidades", 3.8)
        };
    

With the keys {} you are initializing the ArrayList with the indicated values. If you add a ; before what you are doing is instantiate a ArrayList empty and then execute an incorrect command ({new CEstudiante ...})

  • In the foreach final you are printing the whole object r , when (I understand) that what you would want is to print its properties:

    Console.WriteLine(r.Promedio); //no sé cómo se llaman el resto de propiedades de la clase CEstudiante

  • And finally, why are you doing this?

    var estL = estudiantes.OfType<CEstudiante>();

It would be simpler (and correct) to directly create a List of CEstudiante like this:

       List<CEstudiante> estudiantes = new List<CEstudiante>(){
            new CEstudiante("Jhon", "A203", "Telemática", 3.6),
            new CEstudiante("Ana", "B304", "Informática", 4.2),
            new CEstudiante("Pedro", "C405", "Psicología", 2.6),
            new CEstudiante("Erick", "D506", "Escritura", 4.9),
            new CEstudiante("Jaime", "E607", "Ingles", 1.6),
            new CEstudiante("Juan", "F708", "Humanidades", 3.8)
        };

Edit By request of @ Orlando-de-la-rosa I add this link to as reference to the CS1002 error:

  

The compiler detected that a semicolon is missing. A semicolon is required at the end of each of the C # instructions. An instruction can cover more than one line.

As @Pikoh says in his comment:

  

Do not use ArrayList, it has not been deleted from .net by   retrocompatibility, but it is obsolete. Always use List<T>

I leave it here to make it more visible

    
answered by 29.11.2018 в 15:33