views:

718

answers:

11

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.

A: 

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.

RKitson
+3  A: 

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.

Bill Ayakatubby
Ah ha! Thanks for the help!
Mike Wills
I think you're right. It's pretty pointless though. It will convert "198".Substring(2).PadLeft(2, '0') to "08". So it's a protection against three-digit years?!
Chris
A: 

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

blowdart
A: 

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).

Guy Starbuck
+2  A: 

This prints "98" to the console.

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

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.

John Rudy
+1  A: 

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.

Kyralessa
A: 

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.

Chris
A: 

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?

Kevin
A: 

Short answer: "12007" returns "007". "2007" returns "07". "207" returns "07". "07" returns "00".

Long answer: I'd recommend testing it out in a development project instead of the production application, even if you have to make a small test application.

Substring(2) gets the all characters starting at the 3rd position. If the string is "2007" it return "07". If it's "07" it returns "".

PadLeft(2,'0') returns a string that is at least two characters wide. If the original string isn't at least two characters wide, it adds '0' characters to the left of the string until it is. If the string is "" it returns "00". If the string is "7" it returns "07". If the string is "17" it returns "17". If the string is "2007" it returns "2007".

Greg
A: 

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.

Austin Salonen