简体   繁体   中英

Creating a callback in C# to read lines from a text file

I have a parameter called FileName in my program. It has no default value. Whenever a value is set I would like a callback to trigger the reading of the file. I am very new to C# so I have no idea how to create the callback.

The field is initialised as

public string FileName { get; set; }

And whenever its value is set I want to execute the following lines of code

string[] lines = System.IO.File.ReadAllLines(@FileName);

The setting of the variable FileName is done by the user and is handled nicely by the framework I'm using, so its just getting the callback that I need to understand. As I understand it at the moment, because I'm not giving a default value for FileName it is by default Null

As others have stated, it's unclear why you're adamant about setting a value to trigger the reading of a file. The following shows how one can get data from one Form (or class) to another Form (or class).

在此处输入图像描述

For more information, see:

While all three can be used to pass data, the appropriateness of which one to use depends upon what one desires to occur.

Constructor:

Version 1 (Property):

public class Class1
{
    public string Filename { get; private set; }

    public Class1 (string filename)
    {
        Filename = filename;
    }
}

Version 2 (Field):

public class Class1
{
    private string _filename;

    public Class1 (string filename)
    {
        _filename = filename;
    }
}

Method :

Version 1 (Property)

public class Class1
{
    public string Filename { get; private set; }

    public void SetFilename (string filename)
    {
        Filename = filename;
    }
}

Version 2 (Field)

public class Class1
{
    private string _filename;

    public void SetFilename (string filename)
    {
        _filename = filename;
    }
}

Property :

Version 1:

public class Class1
{
    public string Filename { get; set; }
}

Version 2:

public class Class1
{
    private string _filename;

    public string Filename 
    { 
        get
        {
            return _filename;
        }
        set
        {
            _filename = value;
        }
    }
}

Your OP, seems to indicate that you are attempting to do something similar to the following:

WARNING - The following is not recommended :

public class Class1
{
    private string _filename;
    private string[] _lines = null;

    public string Filename 
    { 
        get
        {
            return _filename;
        }
        set
        {
            _filename = value;
             
            //read file
            _lines = System.IO.File.ReadAllLines(@FileName);
        }
    }
}

Depending upon the size of the file, this operation may take some time to complete. Therefore, it's not recommended to place code to read a file within a property setter.

Instead use a method (recommended):

Version 1:

public class Class1
{
    public string Filename { get; private set; }

    public string[] ReadFile (string filename)
    {
        //set value
        Filename = filename;

       //read file
       return System.IO.File.ReadAllLines(filename);
    }
}

Version 2:

public class Class1
{
    public string Filename { get; private set; }
    public string[] Lines { get; private set; } = null;

    public bool ReadFile(string filename)
    {
        try
        {
            //set value
            Filename = filename;

            //read file
            Lines = System.IO.File.ReadAllLines(filename);
            return true;
        }
        catch (Exception ex)
        {
            //ToDo: add desired code
            throw;
        }
    }
}

Version 3:

public class Class1
{
    public string Filename { get; private set; }
    public string[] Lines { get; private set; } = null;

    public void ReadFile (string filename)
    {
        //set value
        Filename = filename;

       //read file
       Lines = System.IO.File.ReadAllLines(filename);
    }
}

Resources:

I think you want this? You just change the default setter and getter.

private string fileName;

public string FileName 
{ 
   get
   { 
      return fileName; 
   } 
   set 
   { 
      fileName = value; 
      string[] lines = System.IO.File.ReadAllLines(@FileName);
   }
}

But maybe this is a bad idea, read the comments:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM