Collections - ArrayList not referencing correct object

G

Guest

The following code only works as expected if a delay is introduced. As is all
values in the ArrayList are the same. I've tried using other types of
collections, can anyone explain what is happening and is it by design?

using System;
using System.Collections;

namespace CollectionTest
{
public class Bet
{
public ArrayList Selection = new ArrayList();

public Bet()
{
Random x = new Random();
Selection.Add(x.NextDouble());
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList Bets = new ArrayList();

// construct some bets
for(int x=0; x<10; x++)
{
Bet b = new Bet();
Bets.Add(b);
//System.Threading.Thread.Sleep(10);
}

foreach(Bet b in Bets)
{
Console.WriteLine(b.Selection[0].ToString());
}

Console.ReadLine();
}
}
}
 
L

Lewis Harvey

In this line here:
Console.WriteLine(b.Selection[0].ToString());

you write out the same value everytime you go through the loop???

Index zero of the array, you need to reference each index.

Regards,



Lewis Harvey

ronnoco said:
The following code only works as expected if a delay is introduced. As is
all
values in the ArrayList are the same. I've tried using other types of
collections, can anyone explain what is happening and is it by design?

using System;
using System.Collections;

namespace CollectionTest
{
public class Bet
{
public ArrayList Selection = new ArrayList();

public Bet()
{
Random x = new Random();
Selection.Add(x.NextDouble());
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList Bets = new ArrayList();

// construct some bets
for(int x=0; x<10; x++)
{
Bet b = new Bet();
Bets.Add(b);
//System.Threading.Thread.Sleep(10);
}

foreach(Bet b in Bets)
{
Console.WriteLine(b.Selection[0].ToString());
}

Console.ReadLine();
}
}
}
 
G

Guest

In the Class Bet I'm only adding one value to the ArrayList Selections to
keep the code simple, in a real situation I would add several values.
Therefore in the main() routine I'm printing out only the first value in
Selection for each Bet.

Lewis Harvey said:
In this line here:
Console.WriteLine(b.Selection[0].ToString());

you write out the same value everytime you go through the loop???

Index zero of the array, you need to reference each index.

Regards,



Lewis Harvey

ronnoco said:
The following code only works as expected if a delay is introduced. As is
all
values in the ArrayList are the same. I've tried using other types of
collections, can anyone explain what is happening and is it by design?

using System;
using System.Collections;

namespace CollectionTest
{
public class Bet
{
public ArrayList Selection = new ArrayList();

public Bet()
{
Random x = new Random();
Selection.Add(x.NextDouble());
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList Bets = new ArrayList();

// construct some bets
for(int x=0; x<10; x++)
{
Bet b = new Bet();
Bets.Add(b);
//System.Threading.Thread.Sleep(10);
}

foreach(Bet b in Bets)
{
Console.WriteLine(b.Selection[0].ToString());
}

Console.ReadLine();
}
}
}
 
G

Guest

I was looking at totally the wrong thing, not realising that the empty
constructor for random() used a time-dependent seed and I guess the time
interval between calls is not sufficient to generate a different seed. Any
delay introduced anywhere would generate different numbers. Thanks


ronnoco said:
In the Class Bet I'm only adding one value to the ArrayList Selections to
keep the code simple, in a real situation I would add several values.
Therefore in the main() routine I'm printing out only the first value in
Selection for each Bet.

Lewis Harvey said:
In this line here:
Console.WriteLine(b.Selection[0].ToString());

you write out the same value everytime you go through the loop???

Index zero of the array, you need to reference each index.

Regards,



Lewis Harvey

ronnoco said:
The following code only works as expected if a delay is introduced. As is
all
values in the ArrayList are the same. I've tried using other types of
collections, can anyone explain what is happening and is it by design?

using System;
using System.Collections;

namespace CollectionTest
{
public class Bet
{
public ArrayList Selection = new ArrayList();

public Bet()
{
Random x = new Random();
Selection.Add(x.NextDouble());
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList Bets = new ArrayList();

// construct some bets
for(int x=0; x<10; x++)
{
Bet b = new Bet();
Bets.Add(b);
//System.Threading.Thread.Sleep(10);
}

foreach(Bet b in Bets)
{
Console.WriteLine(b.Selection[0].ToString());
}

Console.ReadLine();
}
}
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top