1

I have trying to mirror the output screen to .txt file.By my below code i can able to mirror the output screen to text file. When executing the obj.OutputFile("First text"); there is no problem But some times i need print like obj.OutputFile("Second text {0}",text);

I got the exception during the second line execution

No overload for method 'OutputFile' takes 2 arguments test document

How do i clear my exception?

I want to my code which is to be accept different number of arguments passing.

My Code

class Program
    {

        static void Main(string[] args)
        {
            string text = "Sample";
            Program obj = new Program();
            obj.OutputFile("First text");             
            obj.OutputFile("Second text {0}",text);    
            Console.ReadKey();    
        }
        public void OutputFile(string text)
        {

            string path = "Example.txt";

            if (!File.Exists(path))
            {
                using (TextWriter tw = new StreamWriter(path))
                {
                    tw.WriteLine(text);
                    Console.WriteLine(text);
                }
            }    
            else if (File.Exists(path))
            {
                using (TextWriter tw = new StreamWriter(path,true))
                {
                    tw.WriteLine(text);
                    Console.WriteLine(text);
                }
            }
        }         
    }

I am totally new to this c#. So i hope your answer would be simple.

MarkusEgle
  • 2,795
  • 4
  • 41
  • 61
  • 1
    obj.OutputFile(string.Format("Second text {0}",text)); – ColinM Nov 26 '16 at 17:06
  • 1
    Your OutputFile method expects 1 parameter. You've given it 2 (separated by commas) and the compiler is confused. – byxor Nov 26 '16 at 17:07
  • 2
    Totally unrelated to your issue, you could avoid the if /else if block by noting that when `File.Exists` is true, you give `true` as 2nd argument of `Streamwriter` ctor otherwise (when `File.Exists` is false) you give nothing (so you're indirectly calling the `Streamwriter` ctor for which that 2nd argument is `false`) – Sehnsucht Nov 26 '16 at 17:11

2 Answers2

4

Your call for OutputFile doesn't seem correct. You have obj.OutputFile("Second text {0}",text); while the method signature is public void OutputFile(string text), which means it requires one parameter.

All you have to do is to change your call to:

obj.OutputFile(string.Format("Second text {0}", text));

And if you are using C# 6 you can make it even better:

obj.OutputFile($"Second text {text}");
Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
1

change

obj.OutputFile("Second text {0}",text); 

to

obj.OutputFile(string.Format("Second text {0}",text)); 

EDIT:

Your function definition for OutputFile has one parameter. With the comma between the strings you have two parameters instead of the expected one parameter.

MarkusEgle
  • 2,795
  • 4
  • 41
  • 61
  • You could add a quick description at the end explaining why their line of code was wrong. – byxor Nov 26 '16 at 17:07