Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新类时,如何更新显示文本长度的属性?_C# - Fatal编程技术网

C# 更新类时,如何更新显示文本长度的属性?

C# 更新类时,如何更新显示文本长度的属性?,c#,C#,我有以下课程。注:有些关键值未显示: namespace Storage.Models { public abstract class AuditableTable { public string Title { get; set; } public string Text { get; set; } } } 我想将Text属性的长度存储在一个名为TextLength的变量中。我是否可以在创建类实例或更新类时自动执行此操作?您只需添加

我有以下课程。注:有些关键值未显示:

namespace Storage.Models
{
    public abstract class AuditableTable 
    {
        public string Title { get; set; }
        public string Text { get; set; }
    }

}

我想将Text属性的长度存储在一个名为TextLength的变量中。我是否可以在创建类实例或更新类时自动执行此操作?

您只需添加一个带有getter的属性即可:

public abstract class AuditableTable 
{
    public string Title { get; set; }
    public string Text { get; set; }

    public int TextLength
    {
        get { return this.Text.Length; }
    }
}

您只需添加一个具有getter的属性:

public abstract class AuditableTable 
{
    public string Title { get; set; }
    public string Text { get; set; }

    public int TextLength
    {
        get { return this.Text.Length; }
    }
}

您不一定需要属性,除非您希望记录初始值:

    public int TextLength
    {
        get
        {
            return this.Text.Length;
        }
    }
但是,如果确实要记录初始长度,可以执行以下操作:

    string m_Text;

    public string Text
    {
        get
        {
            return m_Text;
        }
        set
        {
            m_Text = value;
            if (m_TextLength == 0)
            {
                m_TextLength = value.Length;
            }
        }
    }

    private int m_TextLength;

    public int TextLength
    {
        get
        {
            return m_TextLength;
        }
    }

您不一定需要属性,除非您希望记录初始值:

    public int TextLength
    {
        get
        {
            return this.Text.Length;
        }
    }
但是,如果确实要记录初始长度,可以执行以下操作:

    string m_Text;

    public string Text
    {
        get
        {
            return m_Text;
        }
        set
        {
            m_Text = value;
            if (m_TextLength == 0)
            {
                m_TextLength = value.Length;
            }
        }
    }

    private int m_TextLength;

    public int TextLength
    {
        get
        {
            return m_TextLength;
        }
    }

我想运行包含长度列的报告。您认为这样做可以吗?还是应该在存储数据时设置值?除非您的报告引擎有一些奇怪的怪癖,否则这样做可能可以。我希望运行包含长度列的报告。您认为这样做可以吗?还是应该在存储数据时设置值?除非您的报告引擎有一些奇怪的怪癖,否则这样做可能是可以的。