这个名字不是“;t存在于当前上下文错误中
本文关键字:存在 于当前 上下文 错误 | 更新日期: 2023-09-27 18:26:16
在public static void ammount()
中,我得到一个错误:"名称"mps"在当前上下文中不存在"如何修复此错误?
struct mp3players{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
public static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
ArrayList mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Make: " + value.make);
Console.WriteLine("Model: " + value.model);
Console.WriteLine("MBSize: " + value.mbsize);
Console.WriteLine("Price: " + value.price);
Console.WriteLine();
}
}
这就是发生错误的地方
我尝试了几种方法,但没有找到我要找的东西。
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
我在C#
方面不是很好,所以我做错了什么也很欢迎解释!
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
该代码具有CCD_ 3的知识。
您可以在方法中添加一个参数,使public static void ammount()
看起来像public static void ammount(mp3players[] mps)
。将修复此错误,但会导致另一个
您必须将mps传递到该方法中。
mps
是mp3()
方法中的局部变量。它不是全局变量,因此在ammount()
方法中不可访问。
我明白你的意思了,你得到了编译错误The name 'mps' does not exist in the current context
。因此,要解决此问题,必须将mps
设置为全局变量,并在mp3()
方法中设置该值,并在其他方法中使用该值。
结构mp3players{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
private static ArrayList mps;
public static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Make: " + value.make);
Console.WriteLine("Model: " + value.model);
Console.WriteLine("MBSize: " + value.mbsize);
Console.WriteLine("Price: " + value.price);
Console.WriteLine();
}
}
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
函数public static void mp3()
内部声明的变量"mps",并且您正试图从另一个函数public static void ammount()
访问该变量,但由于作用域的原因,这是不可行的。
要访问"mps",您必须从mp3()
函数中删除声明,并将其直接移动到您定义的类中:
public class MySongColl
{
private static ArrayList mps = new ArrayList();
struct mp3players
{
....
....
}
public static void mp3()
{
....
....
}
public static void ammount()
{
....
....
}
}
注意:编写代码时请使用正确的命名约定。例如:函数名称ammunt()中的拼写错误应该是amount()。在声明结构mp3players等名称时使用camel-case。这些对于构建代码来说不是必需的,但对于开发人员来说是必不可少的