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

• ### Question

• 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)]```
Wednesday, January 8, 2020 5:17 PM

• Solution

```private double GetDistancebtween(int x1, int y1, int x2, int y2)
{
return Math.Sqrt(Math.Pow((x1 - x2), 2) + Math.Pow((y1 - y2), 2)); ;
}
public IStation FindNearestStation(int x, int y)
{
double ? minDist = null;
Station minStation=null;
foreach (var station in _stations)
{
double dis = GetDistancebtween(x1: station.X, y1: station.Y, x2: x, y2: y);
if (!minDist.HasValue || dis < minDist.Value)
{
minDist = dis;
minStation = station;
}
}
return minStation;
}
```

• Marked as answer by Monday, January 13, 2020 5:10 PM
Monday, January 13, 2020 5:10 PM

### All replies

•  Math.Min(minDist.Value, dis);
Wednesday, January 8, 2020 7:15 PM
• How did you expect this to work?  You are computing the minimum and then throwing the result away.  It's like doing

3 + 4;

and expecting to find 7 somewhere.  You want

minDist = Math.Min(minDist,dis);

However, in your case, Math.Min is not the right solution.  It would work if all you wanted to know was the minimum distance, but You don't really care what the minimum is.  You are returning the station, and right now, you're not remembering that.  You should be doing this instead:

if( !minDist.HasValue  ||  dis < minDist )
{
minDist = dis;
minStation = station;
}

Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

Wednesday, January 8, 2020 7:44 PM
• Hello guys, thanks for help. In defense of my logic. When I use if else statement. If part check the when there is no value for min distance in the case when there is only one station (in this situation the minStation is the first one) and else part checks when we have a station in the list and new station is add it  so I need to compare min distance of the first station to min distance.

I have modified my code but the unit test breaks always on the line 9

`c.FindNearestStation(10, 10).Should().BeSameAs(s1);`

My code modification

``` if(!minDist.HasValue)
{
minDist = dis;
minStation = station;
}
else
{
minDist = Math.Min(minDist.Value, dis);
minStation = station;
}
}
return minStation; ```

• Edited by Thursday, January 9, 2020 9:45 AM New error
Thursday, January 9, 2020 8:53 AM
• Hi BarbraFleg,

Thank you for posting here.

Sometimes it is difficult to fully understand the logic of a piece of code just by looking and thinking.
Not to mention that there are many custom classes and methods in the code you give.

Could you please provide a complete piece of code that we can test directly, which will help us quickly understand your problem?

Best Regards,

Timon

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Friday, January 10, 2020 6:26 AM
• It is too compllex and already resolved so I prefer not to copy paste all of it.
Friday, January 10, 2020 8:39 AM
• Hi BarbraFleg,

I am glad to hear that.

If it is possible, could you please publish your solution and mark it as answer? This will greatly help users with similar problems.

Best Regards,

Timon

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Monday, January 13, 2020 3:09 AM
• Solution

```private double GetDistancebtween(int x1, int y1, int x2, int y2)
{
return Math.Sqrt(Math.Pow((x1 - x2), 2) + Math.Pow((y1 - y2), 2)); ;
}
public IStation FindNearestStation(int x, int y)
{
double ? minDist = null;
Station minStation=null;
foreach (var station in _stations)
{
double dis = GetDistancebtween(x1: station.X, y1: station.Y, x2: x, y2: y);
if (!minDist.HasValue || dis < minDist.Value)
{
minDist = dis;
minStation = station;
}
}
return minStation;
}
```

• Marked as answer by Monday, January 13, 2020 5:10 PM
Monday, January 13, 2020 5:10 PM
• Hi BarbraFleg,