NDEIGU
NDEIGU

Reputation: 2623

How do I automatically set the value of a variable when a class object is created?

I have a class:

public class Test
{
    public string Title { get; set; }
    public Size Size { get; set; }
}

which is a part of a Custom Control. And when the developer creates a Test object, I need my class to automatically calculate the Size of the text of the Title property.

I'm having a major brain fail right now, and I have no idea what to even search for. I'm just lost for words.


How do I automagically measure the size of the Title text when the developer creates the test object:

Test test = new Test()
{
    Title = "This is some text.",
};

I have some ideas, but they don't work, and I feel that they're kinda crazy, so I'm not sure if I should post them.

Upvotes: 0

Views: 710

Answers (4)

DWright
DWright

Reputation: 9500

Do it in the constructor:

public class Test
{
    private string _title = null;
    private Size _size = null;

    public Test(String title)
    {
        Title = title;
    }

    public string Title { get { return _title; }; set { _title = value; _size = TextRenderer.MeasureText(this.Title, this.Font); } }

    public readonly Size Size { get; }
}

Upvotes: 0

Josh L.
Josh L.

Reputation: 454

Like this. It'll automatically calculate your size for you whenever you call Size.

public class Test
{
    public string Title { get; set; }
    public readonly Size Size 
    { 
        get
        {
            return TextRenderer.MeasureText(this.Title, this.Font);
        } 
    }
}

Example:

Test test = new Test()
{
    Title = "This is some text.",
};
var result = test.Size; //Should give you your calculated size when called.

Upvotes: 3

pjmolina
pjmolina

Reputation: 316

Option A. Make Size a derived function.

public class Test
{
    public string Title { get; set; }
    public int Size 
    { 
        get
        {
           return (string.IsNullOrEmpty(Title)) ? 0 : Title.Length;
        } 
    }
}

In this way, size is always in sync with Title.

Option B. Intercept Title.set When ever you change the Title, Size is updated.

public class Test
{
    private string _title;
    public string Title 
    { 
        get { return _title; } 
        set
        {
            _title = value;
            Size = (string.IsNullOrEmpty(value)) ? 0 : Title.Length;
        } 
    }
    public int Size { get; set; }   
}

PD. For measuring strings, int should be enough no need for Size (designed for 2D measures).

Upvotes: 0

Craig W.
Craig W.

Reputation: 18155

Do it in the setter of the Title property. When you use that syntax to create an instance of Test you're actually running through the property setter after the object is instantiated.

public class Test
{
    public Size Size { get; set; }

    private string _title;
    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            Size = // calculate size of _title
        }
    }
}

Upvotes: 3

Related Questions