0

The following code in the main is written:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace text_test
{
class Program
{
    static void Main(string[] args)
     {
       txt_program tt = new txt_program();
        string[] output_txt = tt.txt;
    }
}
}

An error appears:

stating cannot convert method group 'txt' to non-delegate type 'string[]'.

Instead of string[] what should I write? The called code looks like this:

(same system calls as the above).

namespace text_test
{

class txt_program

{
    public void txt(string[] args)
    {
        // Take 5 string inputs -> Store them in an array
        // -> Write the array to a text file

        // Define our one ad only variable
        string[] names = new string[5]; // Array to hold the names

        string[] names1 = new string[] { "max", "lars", "john", "iver", "erik" };

        for (int i = 0; i < 5; i++)
        {
            names[i] = names1[i];
        }

        // Write this array to a text file

        StreamWriter SW = new StreamWriter(@"txt.txt");

        for (int i = 0; i < 5; i++)
        {
            SW.WriteLine(names[i]);
        }

        SW.Close();
    }
}
}
mtb
  • 1,350
  • 16
  • 32
  • 1
    Perhaps `tt.txt()` instead of `tt.txt`? @HimBromBeere's edit: `tt.txt(args)` – Keyur PATEL Sep 14 '16 at 09:30
  • It creates the error There is no argument given that corresponds to the required formal parameter 'args' of 'txt_program-txt(string[])'. It is like the main does not understand the that the output from txt is a .txt- file. @KeyurPATEL – Erik Lenstrup Sep 14 '16 at 09:34
  • what you want to do actually? The function `txt` accept parameter `args` but never use it; Moreover, it return nothing (`void`), so you cannot assign that to a `string[]` – Prisoner Sep 14 '16 at 09:40

2 Answers2

1

If you want just to write an array to file

 static void Main(string[] args) {
   string[] namess = new string[] { 
     "max", "lars", "john", "iver", "erik" };

   File.WriteAllLines(@"txt.txt", names);
 }

In case you insist on separated class with stream:

class txt_program {
  // () You don't use "args" in the method
  public void txt(){ 
    string[] names = new string[] { "max", "lars", "john", "iver", "erik" };

    // wrap IDisposable (StreamWriter) into using 
    using (StreamWriter SW = new StreamWriter(@"txt.txt")) {
      // do not use magic numbers - 5. 
      // You want write all items, don't you? Then write them  
      foreach (var name in names)
        SW.WriteLine(name);
    }
  }
}

...

static void Main(string[] args){
  // create an instance and call the method
  new txt_program().txt();
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

public void txt(string[] args) { }

remove argument "string[] args", no needed.

call method like this tt.txt();

void method doesn't return any value

so don't try to get string array.

yogeswaran K
  • 2,278
  • 8
  • 29
  • 45