Dynamic array

  • Thread starter Thread starter Cleber Schönhofen
  • Start date Start date
C

Cleber Schönhofen

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 = new DadosDoTime();
tt.pos = 0;
tt.classificacao = 0;
i++;
}

its don´t work :

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

why ?
 
    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<DadosDoTime> instead:

List<DadosDoTime> tt = new List<DadosDoTime>();
while (rd.Read())
{
DadosDoTime ddt = new DadosDoTime();
ddt.pos = 0;
ddt.classificacao = 0;
tt.Add(ddt);
}


Jon
 
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<T> or similar - arrays are (by
definition) fixed size:

List<DadosDoTime> tt = new List<DadosDoTime>();
while(rd.Read()) {
DadosDoTime newObj = new DadosDoTime();
newObj.pos = 0;
newObj.classificacao = 0;
tt.Add(newObj);
}

Marc
 
Compilation error, variable not initializate.
DadosDoTime is a class, how i create a List this ? And how i access this
elements after ?
 
    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
 
Ok thanks...

--
Cleber Schönhofen
Infobola
"Jon Skeet [C# MVP]" <[email protected]> escreveu na mensagem
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top