简体   繁体   English

没有递归的堆栈溢出异常

[英]Stack overflow exception with no recursion

I'm having trouble with a stack overflow exception but I can't tell what's causing the exception to be thrown.我遇到了堆栈溢出异常的问题,但我不知道是什么导致了抛出异常。 I'm using a class library that contains all the methods and objects I need and running it from a console application.我正在使用一个包含我需要的所有方法和对象的类库,并从控制台应用程序运行它。

Any help would be greatly appreciated as this is part of an assignment that is due in a couple of hours.任何帮助将不胜感激,因为这是几个小时后到期的任务的一部分。

Here is my code:这是我的代码:

TrafficIncidentNotificationRadiusCalculator class 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 class 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;
    }
}
}

User class用户类

namespace TrafficIncident
{
public class User
{
    public string userFName
    {
        get { return userFName; }
        set { userFName = value; }
    }

    public string userLName
    {
        get { return userLName; }
        set { userLName = value; }
    }
}
}

UserLocationUpdate class 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; }
    }
}
}

And then this is the console application that the class library is running from:然后这是运行类库的控制台应用程序:

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);
    }
}
}

This is not a right way to create properties, define a private field, then the property itself: In your case it will call recursively the set_latitude() method and cause a stack overflow exception.这不是创建属性、定义私有字段和属性本身的正确方法:在您的情况下,它将递归调用set_latitude()方法并导致堆栈溢出异常。

Wrong:错误的:

public double latitude
{
    get { return latitude; }
    set { latitude = value; }
}

Right:对:

private double latitude

public double Latitude
{
    get { return latitude; }
    set { latitude = value; }
}

Or use Auto-Implemented Properties :或使用自动实现的属性

public double Latitude { get; set; }

Your code starts with a recursive assignment, The first recursion is here :您的代码以递归赋值开始,第一个递归在这里:

public double meters;
public double CONFIGURED_NOTIFICATION_RADIUS
{
    get { return CONFIGURED_NOTIFICATION_RADIUS; }
    set { CONFIGURED_NOTIFICATION_RADIUS = meters; }
}

What's wrong:怎么了:

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.每当您为某个属性设置某个值时,它的 setter 都会触发,并且每当您访问某个属性的值时,setter 都会触发。 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在上述情况下,您在它的 setter 中分配属性值,这将重复触发 setter 并导致您获得异常

See all of your getter and setter are wrong, You should use a backup variable or else use them as {get;set} .看到所有的 getter 和 setter 都是错误的,您应该使用备份变量,否则将它们用作{get;set} In the case of userNotificationId you should define the property as like the following:userNotificationId的情况下,您应该将属性定义如下:

private _UserNotificationId
public string UserNotificationId
{
    get { return _UserNotificationId; }
    set { _UserNotificationId= value; }
}

Or simply或者干脆

public string UserNotificationId { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM