2014-09-12 28 views
-1

我是新來的,我按照指示一步一步,但由於某種原因不斷得到stackoverflowexception。我究竟做錯了什麼?得到一個stackoverflowexception並不知道爲什麼。任何人都可以幫忙嗎?

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace assignment3 
{ 
    class changeValue 
    { 
     //Create a class called changeValue that declares 2 integer class variables: value1 and 
     // value2. These should be declared as public and you should not use automatic properties 
     // to declare them. 
     public int value1 
     { 
      get 
      { 
       return value1; 
      }//end get 
      set 
      { 
       value1 = value; 
      }//end set 
     } 
     public int value2 
     { 
      get 
      { 
       return value2; 
      }//end get 
      set 
      { 
       value2 = value; 
      }//end set 
     } 

    public changeValue(int val1, int val2) 
    { 
     //here is the constructor where you code the if statements 
     int value1 = val1; 
     int value2 = val2; 

     if (value1 > 5) 
     { 
      value1 = val1; 
     } 
     if (val1 <= 5) 
     { 
      value1 = (val1+val2); 
     } 
     if (val2 < 10) 
     { 
      value2 = (val2 * val2 + 5); 
     } 
     if (val2 >= 10) 
     { 
      value2 = val2; 
     } 
    } 

    public void printit() 
    { 
     //here is the printit method used to print the results 
     Console.WriteLine("The calculated value is:" + (value1 * value2)); 

    } 
} 
class assignment3 
{ 
    public static void Main(string[] args) 
    { 
     //declare the local val1 and val2 integer variables 
     int val1; 
     int val2;  

     //prompt the user for input of two integers 
     //don’t forget to convert from the string input to integer 
     Console.Write("Enter an integer value: "); //obtain user input 
     val1 = Convert.ToInt32(Console.ReadLine()); 

     Console.Write("Enter a second integer value: "); //obtain user input 
     val2 = Convert.ToInt32(Console.ReadLine()); 

     //instantiate a changeValue object here 
     changeValue myValue = new changeValue(val1,val2); 

     myValue.printit();//call the object method printit here 
    } 
} 
} 
+9

爲'value1'返回'value1',這就要求'value1'吸氣,返回吸氣'value1'它調用'value1'的getter ... – 2014-09-12 13:51:24

+0

在不到三分鐘內六個答案...哇。 – 2014-09-12 13:53:15

+0

10分鐘內最多答案的記錄是多少。 – DidIReallyWriteThat 2014-09-12 13:54:19

回答

1

你創造什麼本質上是一個循環引用。你的getter和setter需要支持者領域。

private int _value1; 
private int _value2; 

public int Value1 
    { 
     get 
     { 
      return _value1; 
     }//end get 
     set 
     { 
      _value1= value; 
     }//end set 
    } 
    public int Value2 
    { 
     get 
     { 
      return _value2; 
     }//end get 
     set 
     { 
      _value2 = value; 
     }//end set 
    } 

通過設置value1 = value的方式,您創建了一個無限循環。

+0

好吧,現在我更好地理解它,爲什麼這本該死的教科書解釋不了更好的東西?大聲笑。 – kidkuk 2014-09-12 14:06:56

+0

我想如果你有一本教科書,你忽略了某些事情。也許它使用了大寫字母V和小寫字母V.我不認爲你發佈的確切代碼是在教科書中。如果是這樣,我會找到一本不同的書來教我編碼。 – DidIReallyWriteThat 2014-09-12 14:07:57

+0

謝謝大家的幫助。但現在當它計算出來時,計算出來的值始終爲0. – kidkuk 2014-09-12 14:08:01

4

這些是你的問題,它們是自我引用。

public int value1 
    { 
     get 
     { 
      return value1; 
     }//end get 
     set 
     { 
      value1 = value; 
     }//end set 
    } 
    public int value2 
    { 
     get 
     { 
      return value2; 
     }//end get 
     set 
     { 
      value2 = value; 
     }//end set 
    } 

他們更改爲:

public int value1 { get; set; } 
    public int value2 { get; set; } 
4

value1的獲取方正在調用自身,它會無限次地遞歸,直到調用堆棧已滿爲止。

不使用自動屬性(這是你是基於代碼中的註釋分配的)的方法是使用一個單獨支持字段:

private int _value1; 

    public int value1 
    { 
     get 
     { 
      return _value1; 
     }//end get 
     set 
     { 
      _value1 = value; 
     }//end set 
    } 
1

你的屬性在changeValue會造成StackOverflowException

public int value1 
{ 
    get 
    { 
     return value1; // --> this line is referencing the property itself and will StackOverflow! 
    }//end get 
    set 
    { 
     value1 = value; 
    }//end set 
} 

相反,嘗試用一個簡單的自動屬性:

public int value1 { get; set; } 

或者使用後備存儲使用不同的名稱爲您的屬性(注意下劃線):

private int _value1; 
public int value1 
{ 
    get 
    { 
     return _value1; 
    }//end get 
    set 
    { 
     _value1 = value; 
    }//end set 
} 
相關問題