none
Cannot convert method group 'ReadLine' to non-delegate type 'string'. Did you intend to invoke the method?

    Question

  • Hi, new to all this. Trying to write a fairly simple program which will replace a specific line in a file and replace it with another and replace the file after with the temp.

    Getting an error, saying "Cannot convert method group 'ReadLine' to non-delegate type 'string'. Did you intend to invoke the method?" and it is something wrong with the line:

    while ((line = sr.ReadLine) != null);

    in the following code...



    using System;
    using System.IO;

    class Test
    {

        public static void Main()
        {
            string path = @"c:\temp\MyTest.prd";
            string temp = @"c:\temp\tempfile.prd";
            string test = "Text To Replace";
            string line = "This Has Been Replaced";
            StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));
            StreamWriter sw = new StreamWriter(File.Open(temp, FileMode.Create));
            try
            {
                while ((line = sr.ReadLine) != null);
                {
                    line = sr.ReadLine();
                    if (line == test)
                        sw.WriteLine(test);
                    else
                        sw.WriteLine(line);
                }
                {
                    sw.Close();
                    sr.Close();
                }

                }
            catch (Exception e)
                {
                    Console.WriteLine ("The process failed: (0)", e.ToString());
                }
        }
            }



    Any help appreciated, have spent ages looking it up and can't find an answer.

    Cheers

    Wednesday, January 02, 2008 3:49 PM

Answers

  • missing () in the while condition.

    while ((line = sr.ReadLine()) != null);
    {
    }

    and I dont think you need any sr.ReadLine() within the while loop.

     Brad Boye wrote:
    Hi, new to all this. Trying to write a fairly simple program which will replace a specific line in a file and replace it with another and replace the file after with the temp.

    Getting an error, saying "Cannot convert method group 'ReadLine' to non-delegate type 'string'. Did you intend to invoke the method?" and it is something wrong with the line:

    while ((line = sr.ReadLine) != null);

    in the following code...



    using System;
    using System.IO;

    class Test
    {

        public static void Main()
        {
            string path = @"c:\temp\MyTest.prd";
            string temp = @"c:\temp\tempfile.prd";
            string test = "Text To Replace";
            string line = "This Has Been Replaced";
            StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));
            StreamWriter sw = new StreamWriter(File.Open(temp, FileMode.Create));
            try
            {
                while ((line = sr.ReadLine) != null);
                {
                    line = sr.ReadLine();
                    if (line == test)
                        sw.WriteLine(test);
                    else
                        sw.WriteLine(line);
                }
                {
                    sw.Close();
                    sr.Close();
                }

                }
            catch (Exception e)
                {
                    Console.WriteLine ("The process failed: (0)", e.ToString());
                }
        }
            }



    Any help appreciated, have spent ages looking it up and can't find an answer.

    Cheers

    Wednesday, January 02, 2008 3:58 PM
  •  

    Have you tried

    Code Block

     

    while ((line = sr.ReadLine()) != null);

     

     

     

    Wednesday, January 02, 2008 3:58 PM

All replies

  • missing () in the while condition.

    while ((line = sr.ReadLine()) != null);
    {
    }

    and I dont think you need any sr.ReadLine() within the while loop.

     Brad Boye wrote:
    Hi, new to all this. Trying to write a fairly simple program which will replace a specific line in a file and replace it with another and replace the file after with the temp.

    Getting an error, saying "Cannot convert method group 'ReadLine' to non-delegate type 'string'. Did you intend to invoke the method?" and it is something wrong with the line:

    while ((line = sr.ReadLine) != null);

    in the following code...



    using System;
    using System.IO;

    class Test
    {

        public static void Main()
        {
            string path = @"c:\temp\MyTest.prd";
            string temp = @"c:\temp\tempfile.prd";
            string test = "Text To Replace";
            string line = "This Has Been Replaced";
            StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));
            StreamWriter sw = new StreamWriter(File.Open(temp, FileMode.Create));
            try
            {
                while ((line = sr.ReadLine) != null);
                {
                    line = sr.ReadLine();
                    if (line == test)
                        sw.WriteLine(test);
                    else
                        sw.WriteLine(line);
                }
                {
                    sw.Close();
                    sr.Close();
                }

                }
            catch (Exception e)
                {
                    Console.WriteLine ("The process failed: (0)", e.ToString());
                }
        }
            }



    Any help appreciated, have spent ages looking it up and can't find an answer.

    Cheers

    Wednesday, January 02, 2008 3:58 PM
  •  

    Have you tried

    Code Block

     

    while ((line = sr.ReadLine()) != null);

     

     

     

    Wednesday, January 02, 2008 3:58 PM
  • Thanks, runs now and worked out the failings in my logic. Thankyou muchly.


    using System;
    using System.IO;

    class Test
    {

        public static void Main()
        {
            string path = @"c:\temp\MyTest.prd";
            string temp = @"c:\temp\tempfile.prd";
            string test = "Text To Replace";
            string testrep = "REPLACED";
            string line;
            StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));
            StreamWriter sw = new StreamWriter(File.Open(temp, FileMode.Create));
            try
            {
                while ((line = sr.ReadLine()) != null)
                {
                 
                    if (line == test)
                        sw.WriteLine(testrep);
                    else
                        sw.WriteLine(line);
                }
                {
                    sw.Close();
                    sr.Close();
                }

                }
            catch (Exception e)
                {
                    Console.WriteLine ("The process failed: (0)", e.ToString());
                }
        }
            }
    Wednesday, January 02, 2008 4:16 PM