Question

In this code I am debugging, I have this code snipit:

ddlExpYear.SelectedItem.Value.Substring(2).PadLeft(2, '0');

What does this return? I really can't run this too much as it is part of a live credit card application. The DropDownList as you could imagine from the name contains the 4-digit year.

UPDATE: Thanks everyone. I don't do a lot of .NET development so setting up a quick test isn't as quick for me.

Was it helpful?

Solution

It takes the last two digits of the year and pads the left side with zeroes to a maximum of 2 characters. Looks like a "just in case" for expiration years ending in 08, 07, etc., making sure that the leading zero is present.

OTHER TIPS

This prints "98" to the console.

class Program
{
    static void Main(string[] args)
    {
        Console.Write("1998".Substring(2).PadLeft(2, '0'));
        Console.Read();
    }
}

Of course you can run this. You just can't run it in the application you're debugging. To find out what it's doing, and not just what it looks like it's doing, make a new web application, put in a DropDownList, put a few static years in it, and then put in the code you've mentioned and see what it does. Then you'll know for certain.

something stupid. It's getting the value of the selected item and taking the everything after the first two characters. If that is only one character, then it adds a '0' to the beginning of it, and if it is zero characters, the it returns '00'. The reason I say this is stupid is because if you need the value to be two characters long, why not just set it like that to begin with when you are creating the drop down list?

It looks like it's grabbing the substring from the 3rd character (if 0 based) to the end, then if the substring has a length less than 2 it's making the length equal to 2 by adding 0 to the left side.

PadLeft ensures that you receive at least two characters from the input, padding the input (on the left side) with the appropriate character. So input, in this case, might be 12. You get "12" back. Or input might be 9, in which case, you get "09" back.

This is an example of complex chaining (see "Is there any benefit in Chaining" post) gone awry, and making code appear overly complex.

The substring returns the value with the first two characters skipped, the padleft pads the result with leading zeros:

 string s = "2014";
    MessageBox.Show(s.Substring(2).PadLeft(2, 'x')); //14
    string s2 = "14";
    MessageBox.Show(s2.Substring(2).PadLeft(2, 'x')); //xx

My guess is the code is trying to convert the year to a 2 digit value.

The PadLeft only does something if the user enters a year that is either 2 or 3 digits long.

With a 1-digit year, you get an exception (Subsring errs).

With a 2-digit year (07, 08, etc), it will return 00. I would say this is an error.

With a 3-digit year (207, 208), which the author may have assumed to be typos, it would return the last digit padded with a zero -- 207 -> 07; 208 -> 08.

As long as the user must choose a year and isn't allowed to enter a year, the PadLeft is unnecessary -- the Substring(2) does exactly what you need given a 4-digit year.

This code seems to be trying to grab a 2 digit year from a four digit year (ddlexpyear is the hint)

It takes strings and returns strings, so I will eschew the string delimiters:

  • 1998 -> 98
  • 2000 -> 00
  • 2001 -> 01
  • 2012 -> 12

Problem is that it doesn't do a good job. In these cases, the padding doesn't actually help. Removing the pad code does not affect the cases it gets correct.

So the code works (with or without the pad) for 4 digit years, what does it do for strings of other lengths?

  • null: exception
  • 0: exception
  • 1: exception
  • 2: always returns "00". e.g. the year 49 (when the Jews were expulsed from rome) becomes "00". This is bad.
  • 3: saves the last digit, and puts a "0" in front of it. Correct in 10% of cases (when the second digit is actually a zero, like 304, or 908), but quite wrong in the remainder (like 915, 423, and 110)
  • 5: just saves the 3rd and 4th digits, which is also wrong, "10549" should probably be "49" but is instead "54".
  • as you can expect the problem continues in higher digits.

OK so it's taking the value from the drop down, ABCD

Then it takes the substring from position 2, CD

And then it err, left pads it with 2 zeros if it needs too, CD

Or, if you've just ended X, then it would substring to X and pad to OX

It's taking the last two digits of the year, then pad to the left with a "0".

So 2010 would be 10, 2009 would be 09.

Not sure why the developer didn't just set the value on the dropdown to the last two digits, or why you would need to left pad it (unless you were dealing with years 0-9 AD).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top