过载方法?为什么这个函数不起作用

本文关键字:函数 不起作用 为什么 方法 | 更新日期: 2023-09-27 18:23:38

{
    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 publish,ref price,ref type,ref title,ref author(;说它有无效的参数?

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

过载方法?为什么这个函数不起作用

您将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参数

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

请先阅读 ref(C# 参考(。

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

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

因为write_one_record参数没有声明为"ref",但你将它们作为"ref"传递。

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

如果在调用write_one_record(...)时尝试使用该ref keyword,则在方法声明中也需要它。就像你在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 ,请将其从方法的调用中删除。

要么不使用 ref,要么直接在调用方法时使用的每个参数之前添加"ref"。