2015-08-25 55 views
0

我試圖使用條件語句來檢查值是否等於使用setter進行設置之前的值。我返回了一個StackOverFlowException錯誤。我正在使用另一個.cs文件來執行一般練習的代碼。設置屬性時出現StackOverFlowException

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Job_Classes 
{ 
    class Workers 
    { 
     public string Name 
     { 
      get { return Name; } 
      set 
      { 
       if (Name.CompareTo("Admin") == 0 || Name.CompareTo("Admin") == -1) //Just trying out comparison with the input. 
       { 
        Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails. 
       } 
       else 
       { 
        Name = value; 
        Console.WriteLine("Done."); 
       } 
      } 
     } 
     public Workers() 
     { 
      this.Name = null; 
     } 
     public Workers(string Name) 
     { 
      this.Name = Name; 
     } 
     public string Information() 
     { 
      return String.Format("Name: {0}", Name); 
     }    
    } 
} 

在其他的.cs我執行的代碼是:

Workers Test = new Workers("John"); 
+0

讀取異常跟蹤;它會顯示重複調用.. – user2864740

回答

5

的問題是導致遞歸相同Property內您重新分配:

 public string Name 
    { 
     get { return Name; } 
     set 
     { 
      if (Name.CompareTo("Admin") == 0 || Name.CompareTo("Admin") == -1) //Just trying out comparison with the input. 
      { 
       Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails. 
      } 
      else 
      { 
       Name = value; //StackOverFlow here 
       Console.WriteLine("Done."); 
      } 
     } 
    } 

如果你是驗證它,你可能想嘗試一種不同的方法。

溶液#1(爲Name屬性使用不同的變量)

private string name = ""; 
public string Name 
{ 
    get { return name; } 
    set 
    { 
     if (value != null) //Check for null before validation (or it's up to you how to handle NULL value) 
     { 
      if (value.CompareTo("Admin") == 0 || value.CompareTo("Admin") == -1) //Just trying out comparison with the input. 
      { 
       Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails. 
      } 
      else 
      { 
       name = value; 
       Console.WriteLine("Done."); 
      } 
     } 
    } 
} 

溶液#2(製作set私人和使用單獨的setter方法。

public string Name 
{ 
    get; 
    private set; //Making this private means you can still 
        //access this setter within the class via `this.Name =` (so be careful) 
} 

/// <summary> 
/// Defined a public setter which can be invoked outside 
/// </summary> 
/// <param name="value"></param> 
public void SetName(string value) 
{ 
    if (value != null) //Check for null before validation (or it's up to you how to handle NULL value) 
    { 
     if (value.CompareTo("Admin") == 0 || value.CompareTo("Admin") == -1) //Just trying out comparison with the input. 
     { 
      Console.WriteLine("Invalid Name."); //To see if an invalid input that is not "Admin" fails. 
     } 
     else 
     { 
      Name = value; 
      Console.WriteLine("Done."); 
     } 
    } 
} 

public Workers(string Name) 
{ 
    this.SetName(Name); //be careful making this.Name = Name or else no validation 
} 

public Workers() 
{ 
    this.SetName(null); //be careful with this.Name = null or else no validation 
} 

的缺點第二種方法是在課程Workers內部,設置時應該小心,因爲即使設置爲private,它仍可在Workers內訪問。

+0

愚蠢的問題,但是'私人字符串名稱=「」;'只是作爲一個佔位符,以允許驗證'價值'? – Bauer

+0

是的,在這種情況下,這將是它的實際用途。在C#1.0中,這是獲取/設置屬性的正確方法(有點冗餘,因爲您必須聲明屬性+佔位符變量)。 –

0

實現getter和/或setter函數時,需要使用容器變量作爲屬性後面的值。這可以是一個簡單的變量或由變量或常量的任意組合表示的更精細的狀態。

一個典型模式可能類似於:

class a { 
    private int i; 
    public int I { 
    get { return i; } 
    set { i = value;} 
    } 
} 

在你的情況,可以通過定義一個私有類變量name,並使用在你的getter/setter函數來解決。