Compare current station distance with known min distance (cannot convert from int? to sbyte in Math.Min)?

  • Thread starter Thread starter BarbraFleg
  • Start date Start date
B

BarbraFleg

Guest
I have small issue with a code which is almost done with the code for the previous method that I need to take care of first other function first. The function FindNearestStatio there I need to compare in else part that compares a current station's distance with known min distance to pass the unit test.

public IStation FindNearestStation(int x, int y)
{
//Math.Min()
int? minDist = null;
Station minStation=null;
foreach (var station in _stations)
{
var dis = GetDistancebtween(x1: x, y1: y, x2: station.X, y2: station.Y);
if(!minDist.HasValue)
{
minDist = dis;
minStation = station;
}
else
{
//compare current station distance with known min distance
Math.Min(minDist,dis);
}
}
return minStation;
}
Unit test
public void T2_city_returns_effectively_the_closest_station()
{
ICity c = CityFactory.CreateCity("Paris");

IStation s = c.AddStation("Opera", 0, 0);
IStation s1 = c.AddStation("Chatelet", 10, 10);

c.FindNearestStation(-10, -10).Should().BeSameAs(s);
c.FindNearestStation(10, 10).Should().BeSameAs(s1);
c.FindNearestStation(15, 15).Should().BeSameAs(s1);
}
[TestCase(0, 0, "station1", 2, 0, "station2", 1, 0)]
[TestCase(-2, 0, "station1", 0, 0, "station2", -1, 0)]
[TestCase(0, 2, "station1", 0, 0, "station2", 0, 1)]
[TestCase(-2, 2, "station1", 2, -2, "station2", 0, 0)]

Continue reading...
 
Back
Top