Nogg
Nogg

Reputation: 337

Overload method? Why won't this function work?

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

Can someone compile this (or just paste code into compiler) and tell me why writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author); says it has invalid arguments?

"The best overloaded method match for 'as2.programs.write_one_record(int, int, int, double, string, string, string)' has some invalid arguments.

Upvotes: 0

Views: 423

Answers (6)

Spencer
Spencer

Reputation: 385

Either don't use ref or directly add "ref" before each parameter that you use when you call your method.

Upvotes: 0

Gabe
Gabe

Reputation: 50523

You're missing the ref keyword in each of the arguments

If you're trying to use the ref keyword when calling write_one_record(...) you need it in the method declaration as well. Just like in you did in 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)

If you didn't intend to use ref, remove it from the call of the method.

Upvotes: 0

shenhengbin
shenhengbin

Reputation: 4294

Please read the ref (C# Reference) first .

you need add the ref keyword also to your method parameter

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

Upvotes: 1

M.Babcock
M.Babcock

Reputation: 18965

write_one_record doesn't contain any ref parameters yet in your call to it they all do...

Given the way your method uses the parameters it should be called with:

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

Rather than:

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

Upvotes: 2

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52157

Because write_one_record parameters are not declared as "ref", yet you are passing them as "ref".

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65342

You define write_one_record as

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

-> No ref parameters

but you call it as

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

-> all ref parameters

Upvotes: 3

Related Questions