没有递归的堆栈溢出异常
Stack overflow exception with no recursion
我遇到了堆栈溢出异常的问题,但我不知道是什么导致抛出异常。我正在使用 class 库,其中包含我需要的所有方法和对象,并且 运行 来自控制台应用程序。
任何帮助将不胜感激,因为这是几个小时后到期的作业的一部分。
这是我的代码:
交通事故通知半径计算器class
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;
}
}
}
交通事故报告class
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;
}
}
}
用户class
namespace TrafficIncident
{
public class User
{
public string userFName
{
get { return userFName; }
set { userFName = value; }
}
public string userLName
{
get { return userLName; }
set { userLName = value; }
}
}
}
用户位置更新class
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; }
}
}
}
然后这是 class 库 运行 来自的控制台应用程序:
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);
}
}
}
这不是创建属性、定义私有字段然后定义 属性 本身的正确方法:在您的情况下,它将递归调用 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; }
您的代码以递归赋值开始,第一个递归在这里:
public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
get { return CONFIGURED_NOTIFICATION_RADIUS; }
set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}
怎么了:
Whenever you set some value to a property it's setter will trigger,
and whenever you access the value of a property the setter will
trigger. in the above mentioned case, you are assigning the property
value in it's setter which will repeatedly trigger the setter and
hance you get the exception
看到你所有的 getter 和 setter 都是错误的,你应该使用备份变量或者将它们用作 {get;set}
。在 userNotificationId
的情况下,您应该将 属性 定义为如下所示:
private _UserNotificationId
public string UserNotificationId
{
get { return _UserNotificationId; }
set { _UserNotificationId= value; }
}
或者干脆
public string UserNotificationId { get; set; }
我遇到了堆栈溢出异常的问题,但我不知道是什么导致抛出异常。我正在使用 class 库,其中包含我需要的所有方法和对象,并且 运行 来自控制台应用程序。
任何帮助将不胜感激,因为这是几个小时后到期的作业的一部分。
这是我的代码:
交通事故通知半径计算器class
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;
}
}
}
交通事故报告class
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;
}
}
}
用户class
namespace TrafficIncident
{
public class User
{
public string userFName
{
get { return userFName; }
set { userFName = value; }
}
public string userLName
{
get { return userLName; }
set { userLName = value; }
}
}
}
用户位置更新class
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; }
}
}
}
然后这是 class 库 运行 来自的控制台应用程序:
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);
}
}
}
这不是创建属性、定义私有字段然后定义 属性 本身的正确方法:在您的情况下,它将递归调用 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; }
您的代码以递归赋值开始,第一个递归在这里:
public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
get { return CONFIGURED_NOTIFICATION_RADIUS; }
set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}
怎么了:
Whenever you set some value to a property it's setter will trigger, and whenever you access the value of a property the setter will trigger. in the above mentioned case, you are assigning the property value in it's setter which will repeatedly trigger the setter and hance you get the exception
看到你所有的 getter 和 setter 都是错误的,你应该使用备份变量或者将它们用作 {get;set}
。在 userNotificationId
的情况下,您应该将 属性 定义为如下所示:
private _UserNotificationId
public string UserNotificationId
{
get { return _UserNotificationId; }
set { _UserNotificationId= value; }
}
或者干脆
public string UserNotificationId { get; set; }