C#:Class不包含定义

时间:2017-11-05 13:12:36

标签: c# function delegates

我目前正在尝试通过名称搜索对象数组,并希望输出成为数组的索引。但是,当我尝试调用我的搜索功能时出错(不包含定义)。我不知道如何解决这个问题。

我对编程非常陌生,如果我做错了什么就请原谅我,请尽量保持简单。

class Program
{
    static void Main(string[] args)
    {
        //Initialize array based on number of records
        RecordArray.Init(5);

        //Calling FindByName
        RecordArray.FindByName (RecordArray.rArray, "Name3");
    }
}

public class RecordClass
{
    //Declaring member variables
    public string Name;
    public int Number;
    public string Email;
}

public class RecordArray
{
    public static void Init(int Size)
    {
        //RNG for phone numbers
        Random Rnd = new Random();

        //Creating an array of RecordClass
        RecordClass[] rArray = new RecordClass[Size];

        //Loop through the array
        for(int i = 0; i < rArray.Length; i++)
        {
            rArray[i].Name = "Name" + i;
            rArray[i].Number = Rnd.Next();
            rArray[i].Email = rArray[i].Name + "@gmail.com";
        }
    }

    public static int FindByName(RecordClass[] rArray, string Name)
    {
        //Loop through rArray to find if Name matches. If match, return index. Otherwise, return -1.
        for(int i = 0; i < rArray.Length; i++)
        {
            if (rArray[i].Name == Name)
            {
                return i;
            }
        }

        return -1;
    }
}

}

2 个答案:

答案 0 :(得分:0)

rarray仅在函数中声明,因此您无法访问它。你可以试试这样的东西

public static RecordClass[] Init(int Size)
{
    //RNG for phone numbers
    Random Rnd = new Random();

    //Creating an array of RecordClass
    RecordClass[] rArray = new RecordClass[Size];

    //Loop through the array
    for(int i = 0; i < rArray.Length; i++)
    {
        RecordClass record = new RecordClass();
        record.Name = "Name" + i;
        record.Number = Rnd.Next();
        record.Email = record.Name + "@gmail.com";
        rArray[I] = record;
    }

    return rArray;
}




static void Main(string[] args)
{
    //Initialize array based on number of records

    //Calling FindByName
    RecordArray.FindByName (RecordArray.Init(5), "Name3");
}

答案 1 :(得分:0)

首先,我首先要说几乎不需要编写自己的数组 C#langange和.NET框架支持数组和许多其他集合类型,所以它真的不需要。

话虽如此,以下是您当前代码的错误:
班级RecordArray不包含名为rArray的成员 要使代码正常工作,必须在init函数中分解这一行:

//Creating an array of RecordClass
RecordClass[] rArray = new RecordClass[Size];

分为两个独立的代码行。首先,您必须为rArray创建静态成员,其次,您使用Init方法初始化它:

public class RecordArray
{
    public static RecordClass[] rArray {get; private set;}

    public static void Init(int Size)
    {
        //RNG for phone numbers
        Random Rnd = new Random();

        //Creating an array of RecordClass
        rArray = new RecordClass[Size];

        //Loop through the array
        for(int i = 0; i < rArray.Length; i++)
        {
            rArray[i] = new RecordClass();
            rArray[i].Name = "Name" + i;
            rArray[i].Number = Rnd.Next();
            rArray[i].Email = rArray[i].Name + "@gmail.com";
        }
    }

    public static int FindByName(RecordClass[] rArray, string Name)
    {
        //Loop through rArray to find if Name matches. If match, return index. Otherwise, return -1.
        for(int i = 0; i < rArray.Length; i++)
        {
            if (rArray[i].Name == Name)
            {
                return i;
            }
        }

        return -1;
    }
}