I'm just getting into C#, so forgive me if this is a basic question. I'm building a WPF application using .NET 4.0 and VS 2010.
I have a class in which I'm storing a List of objects; the objects have a Point object as a field. One of the method returns the object in list at a specific location:
public Marker markerAtLocation(Point location) {
foreach (Marker nextMarker in Markers)
if (nextMarker.Location().Equals(location))
return nextMarker;
return null;
}
Setting breakpoints and using Console.WriteLine I've confirmed that nextMarker is valid at the return. However, in the receiver the object is always null:
Marker top = markerAtLocation(new Point(location.X, location.Y + 1));
if (top == null)
Console.WriteLine("Top is null");
else
Console.WriteLine("Top is " + top.ToString());
Marker bottom = markerAtLocation(new Point(location.X, location.Y - 1));
if ((bottom != null) && (bottom.player == otherPlayerType()) && (top != null) && (top.player == otherPlayerType()))
return true;
I don't know what's going wrong here…
Note that I'd initially thought it was an issue with .NET's Point struct using double values. I know in my app the location values will always be integers so I didn't use the .NET Point and created my own:
class Point {
public int X, Y;
public Point(int X, int Y) {
this.X = X;
this.Y = Y;
}
public bool Equals(Point anotherPoint) {
return ((anotherPoint.X == X) && (anotherPoint.Y == Y));
}
}
I'd appreciate any help!
Edit: In response to pbirkoff:
class Grid {
public List<Marker> Markers;
public Grid() {
Markers = new List<Marker>();
}
public Grid(List<Marker> markers) {
this.Markers = markers;
}
public void addMarker(Marker newMarker) {
Markers.Add(newMarker);
}
I've tried the solutions below to the best of my ability but none of them have helped. I'm including a link to the project itself as well as the problem I'm attempting to solve.
Project: http://dl.dropbox.com/u/7828009/ACSLPetteia.zip
Problem: http://wcipeg.com/etc/ACSL/VOL%2030/ACSL%20Petteia_sr_3.doc
Thanks in advance!
I implemented your code as succinctly as I could, making some mocks where appropriate (code below), and I cannot get it to fail. What am I missing in my code sample:
class Program
{
static void Main(string[] args)
{
LoadMarkers();
var location = new Point(45, 45);
Marker top = markerAtLocation(new Point(location.X, location.Y + 1));
if (top == null)
Console.WriteLine("Top is null");
else
Console.WriteLine("Top is " + top.ToString());
Marker bottom = markerAtLocation(new Point(location.X, location.Y - 1));
}
public static List<Marker> Markers = new List<Marker>();
private static void LoadMarkers()
{
for (var q = 0; q < 50; q++)
for (var w = 0; w < 50; w++)
Markers.Add(new Marker(q, w));
}
public static Marker markerAtLocation(Point location)
{
foreach (Marker nextMarker in Markers)
if (nextMarker.Location().Equals(location))
return nextMarker;
return null;
}
}
class Marker
{
private Point _loc;
public Marker(int x, int y) { _loc = new Point(x, y); }
public Point Location() { return _loc; }
}
class Point
{
public int X, Y;
public Point(int X, int Y)
{
this.X = X;
this.Y = Y;
}
public bool Equals(Point anotherPoint)
{
return ((anotherPoint.X == X) && (anotherPoint.Y == Y));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With