C
C#4mo ago
YouWouldnt

✅ HashCode returning different numbers when parsing the same value

I pass 0, 420 into HashCode.Combine, but when they are in different files the hashcode does not return the same number back
21 Replies
Puran Lai
Puran Lai4mo ago
check the spaces and returns first
YouWouldnt
YouWouldnt4mo ago
c#
Debug.WriteLine("Inside Path Find Class: " + HashCode.Combine(0, 420).GetHashCode());
c#
Debug.WriteLine("Inside Path Find Class: " + HashCode.Combine(0, 420).GetHashCode());
Inside PanelTuple Class: -420709743
Inside Path Find Class: -1668504472
Inside FindPath Class: -1668504472
Inside PanelTuple Class: -420709743
Inside PanelTuple Class: -420709743
Inside Path Find Class: -1668504472
Inside FindPath Class: -1668504472
Inside PanelTuple Class: -420709743
thats the outputs im getting
Puran Lai
Puran Lai4mo ago
yeah, that's strange but that does not relate to the files i get different numbers each time, too
reflectronic
reflectronic4mo ago
well, they certainly will be different each time you run the program. the initial state used by the hashcode algorithm is randomly generated when .NET is loaded
MODiX
MODiX4mo ago
reflectronic
REPL Result: Success
new
{
A = HashCode.Combine(0, 420),
B = HashCode.Combine(0, 420),
C = HashCode.Combine(0, 420)
}
new
{
A = HashCode.Combine(0, 420),
B = HashCode.Combine(0, 420),
C = HashCode.Combine(0, 420)
}
Result: <>f__AnonymousType0#1<int, int, int>
{
"a": -1373943265,
"b": -1373943265,
"c": -1373943265
}
{
"a": -1373943265,
"b": -1373943265,
"c": -1373943265
}
Compile: 229.787ms | Execution: 54.598ms | React with ❌ to remove this embed.
reflectronic
reflectronic4mo ago
it's definitely supposed to be the same every time within the same process, though
Puran Lai
Puran Lai4mo ago
got it thx
YouWouldnt
YouWouldnt4mo ago
I know it changes each time the program but it should be the same hashcode in the program
c#
namespace Birds_Eye.Views.Resources.Functions
{
public class panelTuple
{
public double x;
public double y;
private int _hashCode;
public panelTuple(double x, double y)
{
this.x = x;
this.y = y;
_hashCode = HashCode.Combine(x, y);
if (x == 0 && y == 420)
{
Debug.WriteLine("Inside PanelTuple Class: " + HashCode.Combine(x, y).GetHashCode());
}

}
public override int GetHashCode()
{
return _hashCode;
}
}
}
c#
namespace Birds_Eye.Views.Resources.Functions
{
public class panelTuple
{
public double x;
public double y;
private int _hashCode;
public panelTuple(double x, double y)
{
this.x = x;
this.y = y;
_hashCode = HashCode.Combine(x, y);
if (x == 0 && y == 420)
{
Debug.WriteLine("Inside PanelTuple Class: " + HashCode.Combine(x, y).GetHashCode());
}

}
public override int GetHashCode()
{
return _hashCode;
}
}
}
Where i call the pathfinding
c#
public void pathFind()
{
Pathfinding pathFinding = new Pathfinding(NewmarketPanel);
List<PathNode> pathToDestination = pathFinding.findPath(0,420,1201,428);
List<Polyline> debugPath = new List<Polyline>();
List<Avalonia.Point> debugPathPoints = new List<Avalonia.Point>();
if (pathToDestination != null)
{
foreach (PathNode pathNode in pathToDestination)
{
debugPathPoints.Add(new Avalonia.Point(pathNode.x, pathNode.y));
Polyline newObject = new Polyline
{
Stroke = Avalonia.Media.Brushes.Red,
StrokeThickness = 2
};
var n = debugPathPoints.Count;
for (int i = 0; i < n; i++)
{
newObject.Points.Add(debugPathPoints[i]);
}
debugPath.Add(newObject);
}
}
NewmarketPanel.Children.Add(debugPath);
}
c#
public void pathFind()
{
Pathfinding pathFinding = new Pathfinding(NewmarketPanel);
List<PathNode> pathToDestination = pathFinding.findPath(0,420,1201,428);
List<Polyline> debugPath = new List<Polyline>();
List<Avalonia.Point> debugPathPoints = new List<Avalonia.Point>();
if (pathToDestination != null)
{
foreach (PathNode pathNode in pathToDestination)
{
debugPathPoints.Add(new Avalonia.Point(pathNode.x, pathNode.y));
Polyline newObject = new Polyline
{
Stroke = Avalonia.Media.Brushes.Red,
StrokeThickness = 2
};
var n = debugPathPoints.Count;
for (int i = 0; i < n; i++)
{
newObject.Points.Add(debugPathPoints[i]);
}
debugPath.Add(newObject);
}
}
NewmarketPanel.Children.Add(debugPath);
}
Create Tuple
c#

namespace Birds_Eye.Views.Resources.Functions
{
public class panelTuple
{
public double x;
public double y;
private int _hashCode;
public panelTuple(double x, double y)
{
this.x = x;
this.y = y;
_hashCode = HashCode.Combine(x, y);
if (x == 0 && y == 420)
{
Debug.WriteLine("Inside PanelTuple Class: " + HashCode.Combine(x, y).GetHashCode());
}

}
public override int GetHashCode()
{
return _hashCode;
}
}
}
c#

namespace Birds_Eye.Views.Resources.Functions
{
public class panelTuple
{
public double x;
public double y;
private int _hashCode;
public panelTuple(double x, double y)
{
this.x = x;
this.y = y;
_hashCode = HashCode.Combine(x, y);
if (x == 0 && y == 420)
{
Debug.WriteLine("Inside PanelTuple Class: " + HashCode.Combine(x, y).GetHashCode());
}

}
public override int GetHashCode()
{
return _hashCode;
}
}
}
reflectronic
reflectronic4mo ago
the only explanation for HashCode.Combine returning different values for the same objects is that you are not using the same objects
YouWouldnt
YouWouldnt4mo ago
different variables holding the same number would give a different hashcode?
reflectronic
reflectronic4mo ago
are you sure it's the same number i trust that HashCode works properly tbh. it is very unlikely that the problem is with HashCode
YouWouldnt
YouWouldnt4mo ago
c#
List<PathNode> pathToDestination = pathFinding.findPath(0,420,1201,428);
c#
List<PathNode> pathToDestination = pathFinding.findPath(0,420,1201,428);
startX and startY is the first 2
c#
public List<PathNode> findPath(double startPointX, double startPointY, double endPointX, double endPointY)
c#
public List<PathNode> findPath(double startPointX, double startPointY, double endPointX, double endPointY)
its pissing me off lol
reflectronic
reflectronic4mo ago
right, and what does findPath do with startPointX and startPointY
YouWouldnt
YouWouldnt4mo ago
c#
public List<PathNode> findPath(double startPointX, double startPointY, double endPointX, double endPointY)
{
Debug.WriteLine("Inside FindPath Class: " + HashCode.Combine(0,420).GetHashCode());
PathNode startNode = getNode(startPointX, startPointY);
PathNode endNode = getNode(endPointX, endPointY);
{
openList.Add(startNode);

for (int x = 0; x < panelWidth; x++)
{
for (int y = 0; y < panelHeight; y++)
{
PathNode pathNode = getNode(x, y);
pathNode.gCost = int.MaxValue;
pathNode.calculateFCost();
pathNode.cameFromNode = null;
}
}
startNode.gCost = 0;
startNode.hCost = calculatDistanceCost(startNode, endNode);
startNode.calculateFCost();

while(openList.Count > 0)
{
PathNode currentNode = getLowestFCostNode(openList);
if (currentNode == endNode)
{
return finalPath(endNode);
}
openList.Remove(currentNode);
closedList.Add(currentNode);

foreach (PathNode neighbourNode in getNeighbour(currentNode))
{
if (closedList.Contains(neighbourNode)) continue;
double tentativeGCost = currentNode.gCost + calculatDistanceCost(currentNode, neighbourNode);
if (tentativeGCost < currentNode.gCost)
{
neighbourNode.cameFromNode = currentNode;
neighbourNode.gCost = tentativeGCost;
neighbourNode.hCost = calculatDistanceCost(neighbourNode, endNode);
neighbourNode.calculateFCost();

if (!openList.Contains(neighbourNode))
{
openList.Add(neighbourNode);
}
}
}
}
return null;
}
}
c#
public List<PathNode> findPath(double startPointX, double startPointY, double endPointX, double endPointY)
{
Debug.WriteLine("Inside FindPath Class: " + HashCode.Combine(0,420).GetHashCode());
PathNode startNode = getNode(startPointX, startPointY);
PathNode endNode = getNode(endPointX, endPointY);
{
openList.Add(startNode);

for (int x = 0; x < panelWidth; x++)
{
for (int y = 0; y < panelHeight; y++)
{
PathNode pathNode = getNode(x, y);
pathNode.gCost = int.MaxValue;
pathNode.calculateFCost();
pathNode.cameFromNode = null;
}
}
startNode.gCost = 0;
startNode.hCost = calculatDistanceCost(startNode, endNode);
startNode.calculateFCost();

while(openList.Count > 0)
{
PathNode currentNode = getLowestFCostNode(openList);
if (currentNode == endNode)
{
return finalPath(endNode);
}
openList.Remove(currentNode);
closedList.Add(currentNode);

foreach (PathNode neighbourNode in getNeighbour(currentNode))
{
if (closedList.Contains(neighbourNode)) continue;
double tentativeGCost = currentNode.gCost + calculatDistanceCost(currentNode, neighbourNode);
if (tentativeGCost < currentNode.gCost)
{
neighbourNode.cameFromNode = currentNode;
neighbourNode.gCost = tentativeGCost;
neighbourNode.hCost = calculatDistanceCost(neighbourNode, endNode);
neighbourNode.calculateFCost();

if (!openList.Contains(neighbourNode))
{
openList.Add(neighbourNode);
}
}
}
}
return null;
}
}
c#
private PathNode getNode(double xNode, double yNode)
{

return panelNodes[new panelTuple(xNode, yNode)];
}
c#
private PathNode getNode(double xNode, double yNode)
{

return panelNodes[new panelTuple(xNode, yNode)];
}
reflectronic
reflectronic4mo ago
ah. i see the issue int x = 420; and double x = 420; will not have the same hash code HashCode.Combine(0,420) passes ints, while your parameters are all doubles you should try HashCode.Combine(0.0,420.0) (or 0d, 420d, it's the same)
YouWouldnt
YouWouldnt4mo ago
where is that
reflectronic
reflectronic4mo ago
Debug.WriteLine("Inside FindPath Class: " + HashCode.Combine(0,420).GetHashCode());
YouWouldnt
YouWouldnt4mo ago
System.Collections.Generic.KeyNotFoundException: 'The given key 'Birds_Eye.Views.Resources.Functions.panelTuple' was not present in the dictionary.'
reflectronic
reflectronic4mo ago
i am not sure what you did tbh because changing the debug print statement should not have changed that code at all
YouWouldnt
YouWouldnt4mo ago
shall i make the github project public so you can see everything?
reflectronic
reflectronic4mo ago
to be clear, you do not have a problem with your hash codes you think you have a problem with your hash codes because you printed out HashCode.Combine(0, 420) trying to look for an issue when you really should have printed out HashCode.Combine(0.0, 420.0)--since these are the values you're actually using--and that would have shown you that there's no issue, and the hash codes are the same everywhere i do not know why you started doing this. presumably, you have some other issue that you're trying to debug? if so, i am sorry but this isn't what's causing it if you're trying to figure out some other issue--maybe you are having problems with your dictionary--i am happy to help