I am currently trying to search through an array of objects via a name, and wants the output to be the index of the array. However, an error occurred when I tried to call my searching function (Does not contain a definition). I have no idea how to solve this problem.
I am very new to programming so pardon me if I'm doing everything wrong, and please keep things as simple as possible.
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;
}
}
}
rarray is only declared in a function so you wont have access to it. you could try something like this
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");
}
First, let me start by saying that there is almost never a need to write your own array.
The C# langange and .NET framework support arrays and many other collection types, so it's really not needed.
Having said that, here is what's wrong with your current code:
The class RecordArray
does not contain a member called rArray
.
For your code to work, you must break up this line in the init function:
//Creating an array of RecordClass
RecordClass[] rArray = new RecordClass[Size];
into two separate code lines. First, you must create a static member for rArray
, second, you initialize it in the Init
method:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.