0

Is there a better way to do what is done in the code that follows? I added a PadLeft in the constructor because I need the leading zero and 10 characters. The leading zero is stripped off, for some reason, in my call to the API that returns the "wbd10" value. I did this the old way, but was just wondering if there is a better way to do this?

public class wqpWBD10
{
    string _wbd10;

    public string WBD10 
    {
        get { return _wbd10; }
        set { _wbd10 = value.PadLeft(10, '0'); }
    }
    public string WBD10Name { get; set; }
    public string HUC8 { get; set; }
    public string HUC8Name { get; set; }
}
chuck
  • 57
  • 1
  • 6
  • Please align your title to sample code: there is no constructor at all... Also consider simplifying sample to `value.PadLeft(10,'0')` and stating what value you pass in and what result you get/expect. – Alexei Levenkov Jul 19 '13 at 18:14

1 Answers1

1

That looks fine. If value = 1234, you'll set 0000001234 to your local variable.

You may want to check that value is not null, or the call to PadLeft will throw an exception.

public string WBD10 
{
    get { return _wbd10; }
    set
    {
        if (!String.IsNullOrEmpty(value))
            _wbd10 = value.PadLeft(10, '0');

        // If value is null, take some other action (or do nothing)
    }
}

Also, you're dealing with a property setter. The constructor (which I don't see here) would be:

public wqpWBD10()
{
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165