How to handle serial numbers with leading zeros.

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
I need to store the serial numbers used on parts. We assign a series of serial numbers to some of the parts we make. We keep track of which number we use in a log book sorted by part number. Almost all of our serial numbers for aerospace parts take the form of a 4 or 5 digit number that is Laser marked on the part along with our vendor code. It would look like : 08040 - 00012 for serial number 12.

I need suggestions on how to deal with this?

I only need simple calculations to be performed:
StartSerial = previousEndSerial + 1
EndSerial = StartSerial + Quantity - 1
to add records to the dataset. User will input Quantity (number of parts).

Here's my initial Idea:

Store StartSerial and EndSerial as strings in the dataset (SQL2000).
Get the length of the previousEndSerial string.
Convert previousEndSerial to integer.
Do the calculations.
Test the number of digits of the new numbers (if < 10, if <100, if <1000, etc).
Convert to string and add the correct number of leading zeros to get the correct length.

Anyone thinking of different/better logic?

Thanks for looking...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,493
Location
Sydney, Australia
Programming Experience
10+
Converting a string with leading zeroes to an integer is not an issue. You can use the PadLeft method to add the correct number of leading characters to create a String of a specific length.

(1).ToString().PadLeft(5, "0"c) => "00001"
(10).ToString().PadLeft(5, "0"c) => "00010"
(100).ToString().PadLeft(5, "0"c) => "00100"
(1000).ToString().PadLeft(5, "0"c) => "01000"
(10000).ToString().PadLeft(5, "0"c) => "10000"
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Thank You

I was hoping there was something like that. Once again, Thank you for sharing your great knowledge. Much appriciated.

basic code like this...

dceclare variables:

Public StartSerial As Integer
Public EndSerial As Integer
Public Quantity As Integer
Public tStartNumber As String
Public tEndNumber As String
Public iLength As Integer
Public tLength As String

find starting number and length of string:

StartSerial = Convert.ToInt32(PartSerialGrid.Columns("End Number").Value) + 1
tLength = PartSerialGrid.Columns("End Number").Value
iLength = Len(tLength.Trim())

set ending number and fill both with leading zeros to match previous string length.

EndSerial = StartSerial + (Quantity - 1)
tStartNumber = (StartSerial).ToString().PadLeft(iLength, "0"c)
tEndNumber = (EndSerial).ToString().PadLeft(iLength, "0"c)
 
Last edited:
Top Bottom