mist你好,下面是程序代码:
using System;namespace ConsoleApplication1
{
public class SquareSample
{
public void CalcSquare(ref int nSideLength)
{
nSideLength *=nSideLength;
}
}
 
class SquareApp
{
public static void Main()
{
Console.Write("Please Input a number:");
 
string nSquareRef=Console.ReadLine();
 
SquareSample sq = new SquareSample();
sq.CalcSquare(ref System.Convert.ToInt32(nSquareRef));
Console.WriteLine(nSquareRef.ToString());
Console.ReadLine(); }

}
错误的程序行:sq.CalcSquare(ref System.Convert.ToInt32(nSquareRef));

解决方案 »

  1.   

    ref参数需要你传递变量,而你在调用时传递的是System.Convert.ToInt32(nSquareRef),该成下面的方式:
    ...
    SquareSample sq = new SquareSample();
    int intRef = System.Convert.ToInt32(nSquareRef);
    sq.CalcSquare(ref intRef);
    ...
      

  2.   

    谢谢mist的指点!这样确实能调试通过,但我不明白的是:System.Convert.ToInt32(nSquareRef)本身不也是变量吗?为什还要绕下弯呢?
      

  3.   

    System.Convert.ToInt32(nSquareRef)是一个整形值,不是变量.
    int i; // i才是变量