将属性重置为默认/空 [英] Resetting Property to Default/empty

查看:67
本文介绍了将属性重置为默认/空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我的From Project中有一个UserControl,

我在UserControl中有以下代码:

Hello, I have an UserControl in my From Project,

I have this Code in the UserControl:

public partial class iktMarquee : UserControl
{
    #region Properties
    private string _Text = "";//This parameter will change when changing property 'Text' in property manager in a form
    private int charIndex = 0;
    private Color _FontColor = Color.White;

    [Description("Sets Marquee Text"), DefaultValue(""), Category("Vales"), Browsable(true)]

    public string Text
    {
        get { return _Text; }
        set { _Text = value; }
    }

    [Description("Sets Marquee Font Color"), Category("Appearance"), DefaultValue("White"),  Browsable(true)]
    public Color FontColor
    {
        get { return _FontColor; }
        set { _FontColor = value; }
    }

    [Description("Sets Marquee Interval"), Category("Behavior"), DefaultValue(1000),  Browsable(true)]
    public int Interval
    {
        get { return ScrollTimer.Interval; }
        set { ScrollTimer.Interval = value; }
    }
    #endregion

    #region Control Start
    public iktMarquee()
    {
        InitializeComponent();

    }

    private void iktMarquee_Load(object sender, EventArgs e)
    {

    }
    #endregion

    #region Painting
    public void RePaint()
    {
        System.Drawing.Pen myPen;
        myPen = new System.Drawing.Pen(_FontColor);
        System.Drawing.Graphics formGraphics = this.CreateGraphics();
        formGraphics.Clear(this.BackColor);
        formGraphics.DrawString(_Text.Remove(0,charIndex), this.Font, new SolidBrush(this.FontColor), 5,5);
        myPen.Dispose();
        formGraphics.Dispose();

    }
    #endregion

    #region Private Functions
    private void ScrollTimer_Tick(object sender, EventArgs e)
    {
        //charIndex++;
        if (charIndex > _Text.Length)
            charIndex = 0;
        RePaint();
    }

    private void iktMarquee_Resize(object sender, EventArgs e)
    {
        RePaint();
    }
    #endregion

    private void iktMarquee_Paint(object sender, PaintEventArgs e)
    {
        RePaint();
    }
}



然后,我已经构建了项目,然后在UserControl中将"Text"值设置为"Teststring"

当我以调试模式启动时,文本"将重置为'',而应为测试字符串".

我该如何解决?

谢谢!


发表UserControl的完整代码



Then I have built the project, then set the ''Text'' value in UserControl to ''Teststring ''

When I Start in debug mode, the ''Text'' gets reset to '''', while it should be ''Teststring''.

How can I fix this?

Thanks!


Posted full code of UserControl

推荐答案

用户控件已经具有text属性,只是隐藏了.用正确的属性覆盖和修饰,您可以在不添加任何其他字段的情况下使用它:
The user control already has a text property, it''s just hidden. Override and decorate with the correct attributes and you can use it without any added fields etc:
[Bindable(true),
Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
    get { return base.Text; }
    set { base.Text = value; }
}


.NET中没有默认属性值"的概念,因为它几乎不需要作为通用概念.

您应该在您的特定班级上介绍这种行为.只需添加并实现某些方法,例如Reset()SetThisToDefault()ResetThat()或类似的方法.

现在,您的问题有所不同:您的代码示例不完整,因此我无法确定问题出在哪里,但是现在我只能看到Text getter ,而没有看到setter.如果没有设置器,或者设置器是私有的,则无法为属性分配值,该值将始终为空字符串.

另一个缺陷(不会损害功能,但对维护确实很不利):绝对不要硬编码和立即常量,尤其是字符串,当然除了null.特别是,切勿写",始终使用string.Empty.默认值为null.

—SA
There is no a concept of "default property value" in .NET, because it is hardly needed as an universal concept.

You should introduce such behavior on the level of your particular class. Just add and implement some methods like Reset(), SetThisToDefault(), ResetThat() or anything like that to your liking.

Now, your problem is different: you code sample is incomplete, so I cannot tell where is the problem, but right now I can see only the Text getter, and not setter. If there is no setter, or a setter is private, you have no a way to assign a value to the property, it will always remain an empty string.

Another flaw (which does not compromise functionality but really bad for maintenance): never hard-code and immediate constants, especially strings, except null, of course. In particular, never write "", always use string.Empty. The default value is null.

—SA


这篇关于将属性重置为默认/空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆