我遇到堆栈溢出异常的问题,但我无法告诉导致异常的原因。我使用的类库包含了我需要的所有方法和对象,并从控制台应用程序运行它。
任何帮助都将不胜感激,因为这是在几个小时内完成的作业的一部分。
这是我的代码:
TrafficIncidentNotificationRadiusCalculator类
namespace TrafficIncident
{
public class TrafficIncidentNotificationRadiusCalculator
{
public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
get { return CONFIGURED_NOTIFICATION_RADIUS; }
set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}
public List<string> GetNotificationRecipientsList(List<User> users, List<UserLocationUpdate> userLocation, TrafficIncidentReport report)
{
int i = 0;
List<string> userNotificationIds = new List<string>();
while (i < userLocation.Count)
{
UserLocationUpdate userLoc = userLocation.ElementAt(i);
userNotificationIds.Add(userLoc.userNotificationId);
Console.WriteLine(userNotificationIds.ElementAt(i));
i++;
}
return userNotificationIds;
}
}
}
TrafficIncidentReport类
namespace TrafficIncident
{
public class TrafficIncidentReport
{
public double[] incidentLocation;
public double latitude
{
get { return latitude; }
set { latitude = value; }
}
public double longitude
{
get { return longitude; }
set { longitude = value; }
}
public void SetIncidentLocation()
{
incidentLocation = new double[] { latitude, longitude };
}
public double[] GetIncidentLocation()
{
return incidentLocation;
}
}
}
用户类
namespace TrafficIncident
{
public class User
{
public string userFName
{
get { return userFName; }
set { userFName = value; }
}
public string userLName
{
get { return userLName; }
set { userLName = value; }
}
}
}
UserLocationUpdate类
namespace TrafficIncident
{
public class UserLocationUpdate
{
public string userNotificationId
{
get { return userNotificationId; }
set { userNotificationId = value; }
}
public double lastKnownLatitude
{
get { return lastKnownLatitude; }
set { lastKnownLatitude = value; }
}
public double lastKnownLongitude
{
get { return lastKnownLongitude; }
set { lastKnownLongitude = value; }
}
}
}
然后这是运行类库的控制台应用程序:
namespace ClassLibraryTestApp
{
class Program
{
static void Main(string[] args)
{
List<User> users = new List<User>();
List<UserLocationUpdate> userLocation = new List<UserLocationUpdate>();
User user1 = new User();
user1.userFName = "Scott";
user1.userFName = "Gersbank";
users.Add(user1);
User user2 = new User();
user2.userFName = "John";
user2.userFName = "Smith";
users.Add(user2);
User user3 = new User();
user3.userFName = "James";
user3.userFName = "Moore";
users.Add(user3);
UserLocationUpdate user1Location = new UserLocationUpdate();
user1Location.lastKnownLatitude = 0;
user1Location.lastKnownLongitude = 0;
user1Location.userNotificationId = "user1";
userLocation.Add(user1Location);
UserLocationUpdate user2Location = new UserLocationUpdate();
user1Location.lastKnownLatitude = 1;
user1Location.lastKnownLongitude = 1;
user1Location.userNotificationId = "user2";
userLocation.Add(user2Location);
UserLocationUpdate user3Location = new UserLocationUpdate();
user1Location.lastKnownLatitude = 2;
user1Location.lastKnownLongitude = 2;
user1Location.userNotificationId = "user3";
userLocation.Add(user3Location);
TrafficIncidentReport trafficReport = new TrafficIncidentReport();
trafficReport.latitude = 1;
trafficReport.longitude = 1;
trafficReport.SetIncidentLocation();
TrafficIncidentNotificationRadiusCalculator TINRC = new TrafficIncidentNotificationRadiusCalculator();
TINRC.meters = 20000;
TINRC.GetNotificationRecipientsList(users, userLocation, trafficReport);
}
}
}
答案 0 :(得分:5)
这不是创建属性,定义私有字段,然后是属性本身的正确方法:在您的情况下,它将递归调用set_latitude()
方法并导致堆栈溢出异常。
错:
public double latitude
{
get { return latitude; }
set { latitude = value; }
}
右:
private double latitude
public double Latitude
{
get { return latitude; }
set { latitude = value; }
}
或使用Auto-Implemented Properties:
public double Latitude { get; set; }
答案 1 :(得分:1)
您的代码以递归赋值开头,第一次递归就在这里:
public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
get { return CONFIGURED_NOTIFICATION_RADIUS; }
set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}
出了什么问题:
每当你为某个属性设置一些值时,它的setter就会触发, 每当你访问一个属性的值时,setter就会 触发。在上述情况下,您要分配财产 它的值将会反复触发setter和 你得到了例外
查看您的所有getter和setter都是错误的,您应该使用备份变量,或者将它们用作{get;set}
。在userNotificationId
的情况下,您应该像下面这样定义属性:
private _UserNotificationId
public string UserNotificationId
{
get { return _UserNotificationId; }
set { _UserNotificationId= value; }
}
或者只是
public string UserNotificationId { get; set; }