简体   繁体   中英

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

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

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

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.

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. 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

See all of your getter and setter are wrong, You should use a backup variable or else use them as {get;set} . In the case of userNotificationId you should define the property as like the following:

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

Or simply

public string UserNotificationId { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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