简体   繁体   English

在 C# 中创建回调以从文本文件中读取行

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

I have a parameter called FileName in my program.我的程序中有一个名为FileName的参数。 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.我是C#的新手,所以我不知道如何创建回调。

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.变量FileName的设置由用户完成,并由我使用的框架很好地处理,因此它只是获得我需要了解的回调。 As I understand it at the moment, because I'm not giving a default value for FileName it is by default Null据我目前了解,因为我没有为FileName提供默认值,所以默认情况下为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):版本 1(属性):

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

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

Version 2 (Field):版本 2(现场):

public class Class1
{
    private string _filename;

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

Method :方法

Version 1 (Property)版本 1(属性)

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

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

Version 2 (Field)版本 2(现场)

public class Class1
{
    private string _filename;

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

Property :财产:

Version 1:版本 1:

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

Version 2:版本 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:您的 OP 似乎表明您正在尝试执行类似于以下操作的操作:

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:版本 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:版本 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:版本 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.您只需更改默认的 setter 和 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:-)但也许这是个坏主意,请阅读评论:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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