【问题标题】:Trying to access a temporary array in another method尝试以另一种方法访问临时数组
【发布时间】:2015-10-14 06:47:07
【问题描述】:

如果这是一个愚蠢的问题,我很抱歉,但作为编码的初学者,我发现很难记住我创建的变量的限制/界限。我正在尝试在下面的 GetLetters() 方法中创建一个临时数组,但我稍后需要在 EstimateGrade() 方法中访问此信息,以便根据用户“估计成绩”他们的名字。

我收到“当前上下文中不存在名称‘threeLetters’”的错误消息

有没有办法在不创建公共数组的情况下访问threeLetters 数组。

public int[] GetLetters(String userName)
    {
        //Creating an array that will hold the 3 values that determine grade
        int[] threeLetters = new int[3];

        char firstLetter = userName[0];
        threeLetters[0] = userName[0];
        char thirdLetter = userName[2];
        threeLetters[1] = userName[2];
        char fifthLetter = userName[4];
        threeLetters[2] = userName[4];

        if(userName.Length > 5)
        {
            threeLetters = new int[0];
        }

        return threeLetters;
    }

    public int EstimateGrade(int[] grade)
    {
        int sum = (threeLetters[0] + threeLetters[1] + threeLetters[2]) * 85;
        int result = sum % 101;
        return result;
    }

【问题讨论】:

  • 是的。如果您查看 EstimateGrade 方法,您提供的参数没有在该方法中使用,并且正在使用的参数是“threeLetters”,因为它没有在此处声明,所以无法访问。如果您在参数中声明它,您将能够使用它。请记住,当您使用 EstimateGrade 方法并希望从“GetLetters”中获取名称时,您必须将“GetLetters”方法的返回值传递给 EstimateGrade 方法。例如EstimateGrade(GetLetters(用户名));我希望这是有道理的。
  • @GrantWinney 谢谢 :)
  • @SorrelVesper 是的,这是有道理的,而且帮助很大!你居然回答了我的第二个问题哈哈。

标签: c# arrays temporary-objects


【解决方案1】:

threeLetters[]GetLetters() 是本地的,即threeLetters[]GetLetters() 之外无法访问。由于您将threeLetters[] 作为参数传递给EstimateGrade(),别名为grade[],因此将三个字母更改为等级。请参阅下面的代码。

public int EstimateGrade(int[] grade)
{
    int sum = (grade[0] + grade[1] + grade[2]) * 85;
    int result = sum % 101;
    return result;
}

【讨论】:

    【解决方案2】:

    在大多数情况下,您可以通过 {} 大括号推断任何成员的范围。就类而言,访问修饰符为此添加了另一层。

    因此,在您的示例中,所有变量都是方法的本地变量,但不仅仅是它们,{} 中的所有内容:

    public int[] GetLetters(String userName)
    {    
        int[] threeLetters = new int[3];
    
        char firstLetter = userName[0];    
        char thirdLetter = userName[2];
        char fifthLetter = userName[4];
    
        if(userName.Length > 5)
        {
            threeLetters = new int[0];
        }
    
        return threeLetters;
    }
    

    在极少数情况下,您可能需要针对不同情况使用相同名称的变量,这样您就可以添加更多块来分隔成员,每个嵌套块都可以访问其父块的成员:

    public int[] GetLetters(String userName)
    {    
        int[] threeLetters = new int[3];
    
        // some special block 1
        {
            char firstLetter = userName1[0];    
            threeLetters[0] = firstLetter;
        }
    
        // some special block 2
        {
            char firstLetter = userName2[0];  
            threeLetters[1] = firstLetter ;             
        }
    
        if(userName.Length > 5)
        {
            threeLetters = new int[0];
        }
    
        return threeLetters;
    }
    

    默认情况下,在一个类中,所有成员都是该类的私有和本地成员:

    class A
    {
        // private by default 
        static string Foo()
        {
            return "This is class A";
        }
    }
    
    class B
    {
        public void Foo()
        {
            var name = A.Foo(); // error, cannot access, A.Foo must be public
        }
    }
    

    如果您想从其他方法访问这些变量,您需要将threeLetters 定义为您的类的一个字段(或将其移至父/类范围):

    class A
    {
        int[] threeLetters = new int[3];
    }
    

    同样适用于嵌套类,每个嵌套类都可以访问其父类的成员:

    class A
    {
        static string Foo() { return "Class A"; }
    
        public class B
        {
            public static string Foo() { return A.Foo(); } // no error
        }
    }
    
    Console.WriteLine(B.Foo());
    

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 2018-11-07
      • 2020-06-16
      • 2023-04-09
      相关资源
      最近更新 更多