重载方法?为什么这个功能不起作用?

时间:2012-02-07 01:24:02

标签: c#

{
    class Program
    {
        static void Main(string[] args)
        {
            int id = 0, stock = 0, published = 0, newstock;
            double price = 0.00;
            string type = " ", title = " ", author = " ";

            Program inventroy = new Program();
            inventroy.read_one_record(ref id, ref stock, ref published, ref price, ref type, ref title, ref author);

            Console.WriteLine("Update Number In Stock");
            Console.WriteLine("=======================");
            Console.Write("Item ID: ");
            Console.WriteLine(id);
            Console.Write("Item Type: ");
            Console.WriteLine(type);
            Console.Write("Price: ");
            Console.WriteLine(price);
            Console.Write("Number In Stock: ");
            Console.WriteLine(stock);
            Console.Write("Title: ");
            Console.WriteLine(title);
            Console.Write("Author/Artist: ");
            Console.WriteLine(author);
            Console.Write("Published: ");
            Console.WriteLine(published);
            Console.WriteLine("=======================");

            Console.Write("Please Enter New Stock Number: ");
            string line = Console.ReadLine();
            newstock = int.Parse(line);

            Program writeinv = new Program();

            writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author);
        }

        void read_one_record(ref int id, ref int stock, ref int published, ref double price, ref string type, ref string title, ref string author)
        {
            StreamReader myFile = File.OpenText("Inventory.dat");

            id = int.Parse(myFile.ReadLine());
            stock = int.Parse(myFile.ReadLine());
            published = int.Parse(myFile.ReadLine());
            price = double.Parse(myFile.ReadLine());
            type = myFile.ReadLine();
            title = myFile.ReadLine();
            author = myFile.ReadLine();

            myFile.Close();

        }
        void write_one_record(int id, int newstock, int published, double price, string type, string title, string author)
        {
            StreamWriter myFile = new StreamWriter(File.OpenWrite("Inventory.dat"));


            myFile.WriteLine(id);
            myFile.WriteLine(newstock);
            myFile.WriteLine(published);
            myFile.WriteLine(price);
            myFile.WriteLine(type);
            myFile.WriteLine(title);
            myFile.WriteLine(author);

            myFile.Close();
        }
    }
}

有人可以编译这个(或者只是将代码粘贴到编译器中)并告诉我为什么writeinv.write_one_record(ref id,ref newstock,ref published,ref price,ref type,ref title,ref author);说它有无效的论点?

“'as2.programs.write_one_record(int,int,int,double,string,string,string)'的最佳重载方法匹配'有一些无效的参数。

6 个答案:

答案 0 :(得分:3)

将write_one_record定义为

void write_one_record(int id, int newstock, int published, double price, string type, string title, string author)

- >没有ref参数

但你称之为

writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author);

- >所有ref参数

答案 1 :(得分:2)

write_one_record在您调用它时尚未包含任何ref个参数...

鉴于您的方法使用参数的方式,应该使用它来调用:

writeinv.write_one_record(id, newstock, published, price, type, title, author);

而不是:

writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author);

答案 2 :(得分:1)

请先阅读ref (C# Reference)

您还需要将ref关键字添加到方法参数

write_one_record(ref int id, ref int newstock , ....

答案 3 :(得分:0)

您在每个参数中都缺少 ref 关键字

如果您在调用ref时尝试使用keyword write_one_record(...),那么您也需要在方法声明中使用它。就像你在void red_one_record(...)

中所做的那样
void write_one_record(ref int id, ref int newstock, ref int published, ref double price, ref string type, ref string title, ref string author)

如果您不打算使用ref,请将其从方法调用中删除。

答案 4 :(得分:0)

因为write_one_record参数未声明为“ref”,但您将它们作为“ref”传递。

答案 5 :(得分:0)

要么在调用方法时使用的每个参数之前都不使用ref或直接添加“ref”。