Igor
Igor

Reputation: 6255

What format needs to be a property?

ALL,

I have a following code with C#:

public int property
{
    set
    {
         tbText.Text = property.ToString();
    }
    get
    {
         return Convert.ToInt32(tbText.Text);
    }
}

When I go to the properties Window, I see the message:

Input string was not in the right format.

tbText is a TextBox control on which I am trying to get or set value.

Initially the control is empty.

What am I doing wrong?

Thank you.

Upvotes: 1

Views: 294

Answers (2)

canon
canon

Reputation: 41675

You're looking for the value keyword in your setter.

The contextual keyword value is used in the set accessor in ordinary property declarations. It is similar to an input parameter on a method.

public int property
{
    get
    {             
         int defaultVal;
         int.TryParse(tbText.Text, out defaultVal);
         return defaultVal;
    }
    set
    {
         tbText.Text = value.ToString();
    }
}

Upvotes: 5

matthewr
matthewr

Reputation: 4739

When using setters you need to set it to the keyword value:

public int property
{
    set
    {
        tbText.Text = value.ToString();
    }
    get
    {
        return Convert.ToInt32(tbText.Text);
    }
}

So when setting property:

property = 100; // value is equal to whatever you are making property equal.

Hope this helps!

Upvotes: 0

Related Questions