2017-10-13 12 views
-2

スタックオーバーフロー例外に問題がありますが、例外がスローされる原因を特定できません。私は必要なすべてのメソッドとオブジェクトを含むクラスライブラリを使用しており、コンソールアプリケーションから実行しています。再帰のないスタックオーバーフロー例外

これは2時間以内に予定されている課題の一部ですので、助力をいただければ幸いです。ここで

は私のコードです:

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

Userクラス

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

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

UserLその後、このプロパティを作成するための正しい方法ではありません

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

スタックトレース(の一部)を、ここではかなり参考になります。 – Ivar

+1

どの部分で例外がスローされますか? – SeM

+5

*すべてのプロパティが再帰的に表示されます。 (プロパティメソッドのボディを提供する場合は、自分自身を参照するべきではありません) –

答えて

5

、プライベートフィールドを定義し、プロパティ自体:ocationUpdateクラス

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

そして、これはクラスライブラリから実行されているコンソールアプリケーションです:あなたのケースでは、再帰的に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

、最初の再帰はここにある:

何が悪い

:あなたはそれは、セッターがトリガされます だとあなたは、プロパティのセッターに値にアクセスするたびになります トリガープロパティにいくつかの値を設定するたびに

。上記の場合には、その中にあなたがプロパティを割り当てる 値は繰り返し使用すると、例外

があなたのゲッターとセッターのすべてが間違っている参照を取得セッターと ハンスをトリガするセッターだ、あなたはバックアップを使用する必要があります変数を使用するか、{get;set}として使用します。 userNotificationIdの場合は次のようなとしての特性を定義する必要があります。

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

それとも単に

public string UserNotificationId { get; set; } 
関連する問題