Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All

I have a piece of code to create document numbers featuring, 4 variables.

Prefix, Suffix, NextNum and Length.

The first 3 are rather simple. In the past the 4th one I have hard coded but now it needs improvement. One answer is to use a switch statement to use one of a few options the user may have selected but that seems like bad coding.

Code:

C#
list = fa.SelectList("select Prefix, Suffix, NextNum, Length from docnumbers where Form = 'Invoice' limit 1", list);

t0.Text = String.Format("{0}{2:000000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]), list[3][0]);


I have hardcoded that 6 characters must be displayed but now I want it to get it from the Length variable in the database.

Any ideas?

Many thanks
Andrew

Edit: Switch would look like

C#
list = fa.SelectList("select Prefix, Suffix, NextNum, Length from docnumbers where Form = 'Invoice' limit 1", list);

switch (list[3][0])
            {
                case 1:
                    t0.Text = String.Format("{0}{2:0}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 2:
                    t0.Text = String.Format("{0}{2:00}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 3:
                    t0.Text = String.Format("{0}{2:000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 4:
                    t0.Text = String.Format("{0}{2:0000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 5:
                    t0.Text = String.Format("{0}{2:00000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 6:
                    t0.Text = String.Format("{0}{2:000000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 7:
                    t0.Text = String.Format("{0}{2:0000000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 8:
                    t0.Text = String.Format("{0}{2:00000000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 9:
                    t0.Text = String.Format("{0}{2:000000000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                case 10:
                    t0.Text = String.Format("{0}{2:0000000000}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
                default:
                    t0.Text = String.Format("{0}{2}{1}", list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
                    break;
            }
Posted
Updated 14-Jan-12 7:27am
v2
Comments
[no name] 14-Jan-12 12:56pm    
What switch? Your code snippet has no switch statement. Why would using a switch be bad coding?
AspDotNetDev 14-Jan-12 13:51pm    
Mark, FYI, the OP added a comment, but not as a reply to your comment, so I'm sending you this reply so you will be notified.
jinxster 14-Jan-12 20:18pm    
Thanks ASP, Im a bit blind and didnt see the small replies :)
jinxster 14-Jan-12 13:26pm    
A switch would look like the edited bit in the question. It would only support 1-10. It would probably be suffice but it seems limited.

1 solution

You could create a string variable and use it:
string zeroes = new string('0', (list[3][0]));
string testFormat = "{0}{2:" + zeroes + "}{1}";
tx0.Text = String.Format(testFormat, list[0][0], list[1][0], Convert.ToInt32(list[2][0]));
 
Share this answer
 
v2
Comments
jinxster 14-Jan-12 20:20pm    
Thanks thats what I was looking for!
[no name] 14-Jan-12 20:25pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900