Dynamic array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cleber Schönhofen

    Dynamic array

    Hi,
    I need a dynamic array of objects, but don´t work.

    its work :
    DadosDoTime[] tt = new DadosDoTime[100];
    int i = 0;
    while (rd.Read())
    {
    tt[i] = new DadosDoTime();
    tt[i].pos = 0;
    tt[i].classificacao = 0;
    i++;
    }

    its don´t work :

    DadosDoTime[] tt;
    int i = 0;
    while (rd.Read())
    {
    tt[i] = new DadosDoTime();
    tt[i].pos = 0;
    tt[i].classificacao = 0;
    i++;
    }

    why ?

    --
    Cleber Schönhofen
    Infobola


  • Jon Skeet [C# MVP]

    #2
    Re: Dynamic array

    On Sep 26, 1:40 pm, "Cleber Schönhofen" <cleber...@hotm ail.comwrote:
        I need a dynamic array of objects, but don´t work.
    Arrays are always fixed in size in .NET.

    Sounds like you want a List<DadosDoTim einstead:

    List<DadosDoTim ett = new List<DadosDoTim e>();
    while (rd.Read())
    {
    DadosDoTime ddt = new DadosDoTime();
    ddt.pos = 0;
    ddt.classificac ao = 0;
    tt.Add(ddt);
    }


    Jon

    Comment

    • Marc Gravell

      #3
      Re: Dynamic array

      Can you define "its don´t work"?

      What happens? Does it error? Or not work as expected?

      It sounds like you actually want a List<Tor similar - arrays are (by
      definition) fixed size:

      List<DadosDoTim ett = new List<DadosDoTim e>();
      while(rd.Read() ) {
      DadosDoTime newObj = new DadosDoTime();
      newObj.pos = 0;
      newObj.classifi cacao = 0;
      tt.Add(newObj);
      }

      Marc

      Comment

      • Cleber Schönhofen

        #4
        Re: Dynamic array

        Compilation error, variable not initializate.
        DadosDoTime is a class, how i create a List this ? And how i access this
        elements after ?
        --
        Cleber Schönhofen
        Infobola
        "Marc Gravell" <marc.gravell@g mail.comescreve u na mensagem
        news:uuGe5Z9HJH A.4416@TK2MSFTN GP06.phx.gbl...
        Can you define "its don´t work"?
        >
        What happens? Does it error? Or not work as expected?
        >
        It sounds like you actually want a List<Tor similar - arrays are (by
        definition) fixed size:
        >
        List<DadosDoTim ett = new List<DadosDoTim e>();
        while(rd.Read() ) {
        DadosDoTime newObj = new DadosDoTime();
        newObj.pos = 0;
        newObj.classifi cacao = 0;
        tt.Add(newObj);
        }
        >
        Marc

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Dynamic array

          On Sep 26, 2:12 pm, "Cleber Schönhofen" <cleber...@hotm ail.comwrote:
              Compilation error, variable not initializate.
          On which line? The only variable you haven't shown as being
          initialized is "rd" - which has nothing to do with the array side of
          things.
              DadosDoTime is a class, how i create a List this ?
          See Marc's code or mine.
          And how i access this elements after ?
          You can access them by index, or using foreach.

          Jon

          Comment

          • Cleber Schönhofen

            #6
            Re: Dynamic array

            Ok thanks...

            --
            Cleber Schönhofen
            Infobola
            "Jon Skeet [C# MVP]" <skeet@pobox.co mescreveu na mensagem
            news:5808dadc-a6e9-4027-97aa-1fa5b0ce8ebd@s2 0g2000prd.googl egroups.com...
            On Sep 26, 2:12 pm, "Cleber Schönhofen" <cleber...@hotm ail.comwrote:
            Compilation error, variable not initializate.
            On which line? The only variable you haven't shown as being
            initialized is "rd" - which has nothing to do with the array side of
            things.
            DadosDoTime is a class, how i create a List this ?
            See Marc's code or mine.
            And how i access this elements after ?
            You can access them by index, or using foreach.

            Jon


            Comment

            • OD

              #7
              Re: Dynamic array

              its don´t work :
              DadosDoTime[] tt;
              int i = 0;
              while (rd.Read())
              {
              tt[i] = new DadosDoTime();
              tt[i].pos = 0;
              tt[i].classificacao = 0;
              i++;
              }
              >
              why ?
              because tt[i] is pointing nowhere since tt doesn't exist (the array
              itself has not been created, what you're doing ini first version that
              is working...)
              You're mixing item creation and array creation. Second version of your
              code is creating array items, but not the array.

              Another point : your code is dangerous because you're using a fixed
              length array (100 items) and you're looping within a "while (rd.Read)"
              and you can't be sure there are exactly 100 items to read. Perhaps
              less, perhaps more. And for the last case your code will crash.
              Use a List<DadosDoTim einstead of an array.

              --


              OD___



              Comment

              Working...