C# Class Method to loop through a List created in the same Class -
to start off new c# , in need of help. class contains list. can set items in list application not class(where needs done). needing move event class well. works in app well. appreciated. below code class:
 namespace carrace { class cars { public string name { get; set; } public int startpos { get; set; } public int speed { get; set; } public int curpos { get; set; } public double location { get; set; } public cars(string name, int startpos, int speed, int curpos, long location) { this.name = name; this.startpos = startpos; this.speed = speed; this.curpos = 0; this.location = 0; } public static int comparecurrentlocation(cars c1, cars c2) { return c2.location.compareto(c1.location); } } }  i needing add class:
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.attribute("startpos").value), speed = int.parse(x.attribute("speed").value), curpos = 0 }); }  and this:
int p = 1; int prevpos = 1; datetime? prevtime; double dist = 0; double prevloc = 0; if (i == 0) { return; } foreach (var car in racelist) { dist = (((car.speed * 1609.344) * 1) / 1609.344) / 3600; car.location = car.location + dist; } comparison<cars> comploc = cars.comparecurrentlocation; racelist.sort(comploc); prevtime = datetime.now; foreach (var car in racelist) { if (car.location != prevloc) { car.curpos = p; } else { car.curpos = prevpos; } prevpos = car.curpos; prevloc = car.location; p++; }  thanks
thanks of replies. tried car.speed/3600 0 did long way. did working way needed to.
that's because doing integer division (car.speed of type int) fractional part of result discarded. since car's speed less 3600 hence result in zero. can avoid casting car.speed double first - should produce result want:
dist = (double)car.speed / 3600;  
Comments
Post a Comment