Hi all,
I'm trying to access a list in a class made in another class and display it in a listbox but its not working. I most probably have to make a small change to be able to get it to work. So please check in the code if you can see where the problem is.
I have two classes - Race and Runner.
The Race class
Where I created the list and added the runners into it using a method that I call in the main form.
namespace HW2
{
class Race
{
public List<Runner> myList = new List<Runner>();
private double mDistance;
private string mName;
private string mDate;
private string mRunnerList;
public double Distance
{
get { return mDistance; }
set { mDistance = value; }
}
public string Name
{
get { return mName; }
set { mName = value; }
}
public string Date
{
get { return mDate; }
set { mDate = value; }
}
public string RunnerList
{
get { return mRunnerList; }
set { mRunnerList = value; }
}
public void AddRunnerInfo(string name, int number)
{
Runner runA = new Runner();
runA.Name = name;
foreach (Runner run in myList)
{
for (int i = 0; i < myList.Count; i++)
{
runA.RaceNumber = (number + i);
}
}
myList.Add(runA);
//TESTING THE RACENUMBER ASSIGNMENT
MessageBox.Show(Convert.ToString(runA.Name));
MessageBox.Show(Convert.ToString(runA.RaceNumber));
}
}
}
The Runner Class
Where I wish to display the added items in the list in the Display() method
namespace HW2
{
class Runner
{
public string mName;
public int mPosition;
public int mRaceNumber;
public double mTime;
public string Name
{
get { return mName; }
set { mName = value; }
}
public int Position
{
get { return mPosition; }
set { mPosition = value; }
}
public int RaceNumber
{
get { return mRaceNumber; }
set { mRaceNumber = value; }
}
public double Time
{
get { return mTime; }
set { mTime = value; }
}
public Runner()
{
mName = "No Name";
mPosition = 0;
mRaceNumber = 0;
mTime = 0.0;
}
public void Display(Form1 f1)
{
Race r = new Race();
f1.listBox1.Items.Clear();
foreach (Runner runner in r.myList)
{
for (int i = 0; i < r.myList.Count; i++)
{
f1.listBox1.Items.Add(runner.Name + ", " + runner.Position + ", " + runner.RaceNumber + ", " + runner.Time + "\n");
i++;
}
MessageBox.Show(runner.mName);
}
}
}
}
My main Form
Where I call the methods to what they're supposed to do :)
private void button4_Click(object sender, EventArgs e)
{
//Adding a new runner to the race by using the AddRunnerInfo() method
string name;
int number = 1;
//NAME
name = textBox4.Text;
myRunner.Name = name;
//RACENUMBER
myRunner.RaceNumber = number;
//ADD PARAMETERS TO THE METHOD
myRace.AddRunnerInfo(name, number);
//DISPLAY THE RUNNER'S DETAILS
myRunner.Display(this);
}
I am very close but cant seem to find the problem
ANY help will be much appreciated! :)
Joe