0
{
    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.

6
  • What does the error say? Commented Feb 7, 2012 at 1:25
  • Reduce your code example, please. (That is, remove everything except what's necessary to make the error appear.) Commented Feb 7, 2012 at 1:26
  • you need void write_one_record(ref int id, ref int newstock , .... Commented Feb 7, 2012 at 1:26
  • why are you passing your arguments with the ref modifier? Commented Feb 7, 2012 at 1:28
  • Because I don't want to pass them with value? lol... if I need void in front why didn't It prompt an error for the read ? Commented Feb 7, 2012 at 1:29

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This was explained well and I don't know how I missed it. Thank you.
2

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

Comments

1

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 , ....

Comments

0

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

Comments

0

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.

2 Comments

Where are you talking about? I have read and write both exactly the same.
No you don't, you're not including the ref in the method declaration.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.