Hello,
I am such a rookie when it comes to C#. I have created a class with a list. I am needing to read an xml file into this from inside the class. Then I am needing to manipulate this list from inside the class as well. I have everything working in the main part of the application but I am so completely lost when it comes to doing it in the class. I will add the code I am attempting to use below. Please have a look.
//Creating and adding to the list fine in the main app
if (File.Exists("NewRace.xml"))
{
XDocument doc = XDocument.Load("NewRace.xml");
var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0,"")
{
Name = x.Attribute("Name").Value,
StartPOS = int.Parse(x.Element("StartPOS").Value),
CurPOS = 0,
Speed = int.Parse(x.Element("Speed").Value),
Location = double.Parse(x.Element("Location").Value),
Make = x.Attribute("Make").Value
});
foreach (var car1 in nCars)
{
if (ProgPart == "1")
{
rc.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, car1.Speed, car1.Location, car1.Make));
}
else
{
rc.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, 0, car1.Location, car1.Make));
}
}
//Not sure where to put this in the class itself and be able to access this from the main app.
//What I have tried to do in the class to change the data
using System;
using System.Collections.Generic;
using System.Linq;
namespace CarRace
{
class Cars
{
public string Name { get; set; }
public int StartPOS { get; set; }
public int CurPOS { get; set; }
public int Speed { get; set; }
public double Location { get; set; }
public string Make { get; set; }
public Cars(string Name, int StartPOS, int CurPOS, int Speed, double Location, string Make)
{
this.Name = Name;
this.StartPOS = StartPOS;
this.CurPOS = CurPOS;
this.Speed = Speed;
this.Location = Location;
this.Make = Make;
}
public static int CompareCurrentLocation(Cars c1, Cars c2)
{
return c2.Location.CompareTo(c1.Location);
}
public bool GetCurrentLocation(string ProgType, long i)
{
List<Cars> l;
int p = 1;
int prevPOS = 1;
DateTime? PrevTime;
double dist = 0;
double prevLocation = 0;
if (i == 0)
{
return false;
}
foreach (var car in l)
{
if (ProgType == "2")
{
switch (car.Make)
{
case "300ZX":
//Slow continuous gain of speed by percentage after 60 with top speed of 320.
if (i <= 15)
{
car.Speed = car.Speed + (60 / 15);
}
else if (car.Speed < 320)
{
double percent = (double)(car.Speed * .1);
car.Speed = Convert.ToInt32(car.Speed + (percent));
}
break;
case "Camero":
//0-60 4 seconds 60-220 20 seconds Top 220
if (i <= 4)
{
car.Speed = car.Speed + (60 / 4);
}
else if (i <= 20)
{
car.Speed = car.Speed + 10;
}
break;
case "Mustang":
//0-top(210) 25 seconds
if (car.Speed < 200)
{
car.Speed = car.Speed + (200 / 20);
}
break;
case "VW Bug":
//Constant
car.Speed = 165;
break;
default:
Console.WriteLine("Make not found");
break;
}
dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
car.Location = car.Location + dist;
}
}
Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
l.Sort(compLoc);
PrevTime = DateTime.Now;
foreach (var car in l)
{
if (car.Location != prevLocation)
{
car.CurPOS = p;
}
else
{
car.CurPOS = prevPOS;
}
prevPOS = car.CurPOS;
prevLocation = car.Location;
p++;
}
return true;
}
}
}