0

I'm translating some code from VB to C#. The problem is with ArrayList. In VB it runs fine but in C# it doesn't.
I have a Class named 'Numero'. In this 'Numero' there is an ArrayList named 'ArraySorteosAparecidos'.
In this Array 'ArraySorteosAparecidos' there is another object named 'SorteosAparecidos'.
In the loop 'Calcular' I read each object 'Numero' in ArrayList named 'ArrayNumeros'.
Then with the object 'Numero' I need to read one object of array 'ArraySorteosAparecidos' and read a field of this object 'SorteosAparecidos': 'sorteoaparecido1'.
In VB I can read objNumero.ArraySorteosAparecidos1(SorteosAparecidos - 1).sorteoaparecido1 but in C# I cannot.

My VB:

Public Class Numero

    Private Numero As Long
    Private ArraySorteosAparecidos As ArrayList

End Class

Sub Calcular(elNumero As Long, NumeroSorteo As Long)
    Dim objNumero As Numero
    Dim PendientesCalculadas As Long
    Dim SorteosAparecidos As Long
    Dim PendienteUltima As Decimal
    Dim PendienteMedia As Decimal

    For Each objNumero In ArrayNumeros
        If elNumero = objNumero.Numero1 Then
            Dim ElSorteoAparecido = New SorteoAparecido
            Dim ElArrayDelNumero As New ArrayList()

            ElSorteoAparecido.SorteoAparecido1 = NumeroSorteo
            If Not objNumero.ArraySorteosAparecidos1 Is Nothing Then
                ElArrayDelNumero = objNumero.ArraySorteosAparecidos1
            End If

                PendientesCalculadas = objNumero.ArraySorteosAparecidos1.Count - 2
                If PendientesCalculadas >= 0 Then
                    SorteosAparecidos = objNumero.ArraySorteosAparecidos1.Count
                    PendienteUltima = (NumeroSorteo - objNumero.ArraySorteosAparecidos1(SorteosAparecidos - 1).sorteoaparecido1) / (objNumero.ArraySorteosAparecidos1(SorteosAparecidos - 1).sorteoaparecido1 - objNumero.ArraySorteosAparecidos1(SorteosAparecidos - 2).sorteoaparecido1)
                    ElSorteoAparecido.PendienteUltima1 = PendienteUltima                      
                End If

            ElArrayDelNumero.Add(ElSorteoAparecido)               
            objNumero.ArraySorteosAparecidos1 = ElArrayDelNumero
            Exit For
        End If
    Next

End Sub

Now my C#

private void Calcular(long elnumero,long numerosorteo)
{
    long pendientescalculadas;
    int Sorteosaparecidos;
    decimal pendienteultima=0;
    decimal pendientemedia=0;

    foreach (Numero objnumero in arraynumeros) {

        if (elnumero == objnumero.Numeral1 ) {
            SorteoAparecido Elsorteoaparecido = new SorteoAparecido();
            ArrayList Elarraydelnumero = new ArrayList();

            Elsorteoaparecido.SorteoApa1 = numerosorteo;
            if ( objnumero.ArraySorteosAparecidos1 != null) {
                Elarraydelnumero = objnumero.ArraySorteosAparecidos1;
            }

                pendientescalculadas = objnumero.ArraySorteosAparecidos1.Count - 2;
                if (pendientescalculadas >= 0) {
                    Sorteosaparecidos = objnumero.ArraySorteosAparecidos1.Count;
                    Elarraydelnumero = objnumero.ArraySorteosAparecidos1;                            
                    pendienteultima = (numerosorteo - Elarraydelnumero[Sorteosaparecidos - 1].sorteoaparecido1 ) / (Elarraydelnumero[Sorteosaparecidos - 1].sorteoaparecido1 - Elarraydelnumero[Sorteosaparecidos - 2].sorteoaparecido1 );
                    Elsorteoaparecido.PendienteUltima1 = pendienteultima;
                }

            Elarraydelnumero.Add(Elsorteoaparecido);
            objnumero.ArraySorteosAparecidos1 = Elarraydelnumero;
            break;
        }
    }
}

The problem is in

Elarraydelnumero[Sorteosaparecidos - 1].sorteoaparecido1

In VB it's ok but in C# it doesn't run.

How I can do it?

djv
  • 15,168
  • 7
  • 48
  • 72
Karlos Garcia
  • 174
  • 3
  • 16
  • What do you mean by "it doesn't run"? What error are you getting? – germi Oct 25 '19 at 11:46
  • 2
    I think in VB ArrayList is equivalent to List<> in C#. In C# lists are not really lists if I'm right. The framework does some magic with arrays that is why you can index the entities of it. Try with that maybe? – turanszkik Oct 25 '19 at 11:48
  • 2
    @turanszkik is right. `ArrayList` in C# is long deprecated, use a generic `List` instead, (`T` being the class of objects you want to put into the list). – germi Oct 25 '19 at 11:50
  • The error is 'object does not contain a definition for ArraySorteosAparecidos1' – Karlos Garcia Oct 25 '19 at 12:01
  • 1
    It would probably not run in VB if you had the option strict on or something like that. In both case, you need to properly cast the object to it's proper class. – the_lotus Oct 25 '19 at 12:22
  • 1
    `objNumero.ArraySorteosAparecidos1` should be `ArraySorteosAparecidos`, there's a `1` at the end, so yes, `object does not contain a definition for ArraySorteosAparecidos1` is true. Also `objnumero.Numeral1` is wrong as well, because `Numeral1` isn't a member either, it's `Numeral`. Please look at your `Numero` class... – Trevor Oct 25 '19 at 13:23
  • 1
    @KarlosGarcia As the-lotus implied, you need to make the VB.NET code work with [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) to make it easier to convert to C#. – Andrew Morton Oct 25 '19 at 13:45

0 Answers0