该名称在当前上下文错误中不存在

时间:2014-10-11 14:15:32

标签: c# arraylist struct scope

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#我不是很擅长,所以我非常欢迎解释我做错了什么!

4 个答案:

答案 0 :(得分:1)

public static void ammount()
{
    foreach (mp3players value in mps)
    {
        Console.WriteLine("ID: " + value.id);
        Console.WriteLine("Model: " + value.model);
        Console.WriteLine();
    }
}

此代码了解mps

您要为方法添加参数,以便public static void ammount()看起来像public static void ammount(mp3players[] mps)。这将解决此错误,但会导致另一个

你必须将mps传递给方法。

答案 1 :(得分:0)

mpsmp3()方法中的局部变量。它不是全局变量,因此无法在ammount()方法中访问它。

答案 2 :(得分:0)

我明白了,你得到编译错误The name 'mps' does not exist in the current context。所以要解决这个问题,你必须将mps作为全局变量并在mp3()方法中设置该值,并在其他方法中使用它。

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; }
        }

    }
    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();
        }
    }

答案 3 :(得分:0)

在函数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()
    {
        ....
        ....
    }
}

注意: 在编写代码时使用正确的命名约定。例如:函数名称ammount()中的Speeling错误应该是amount()。并且使用驼峰的情况下声明像struct mp3players这样的名字应该是Mp3Players等。这些不是构建代码所必需的,但对于开发人员来说是必不可少的。