How to maintain Zeros in incremented values
ex: ECJ-00001

any help willb appreciated
this is how I am icrementing

Dim Separ As String() = fullString.Split("-")

        Dim StrPart As String = String.Format(Separ(0), "ABC", System.String.Format("SIM"))
        Dim intPart As Integer = Convert.ToInt32(Separ(1))
        Dim Novonumero As String = String.Empty

        Novonumero = String.Format("{0}-{1}", Separ(0), System.Threading.Interlocked.Increment(intPart), 1)
        lblTicketNumber.Text = Novonumero

Thank you

Recommended Answers

All 5 Replies

Try,

Set Label1.Text=ECJ-00001

Add foll. code in click handle of button

  Dim prefix = fullString.Split("-")(0)
  Dim no As Integer = Integer.Parse(fullString.Split("-")(1))
  no = no + 1
  Dim str As New String("0"c, 5 - no.ToString().Length)
  fullString = prefix & "-" & str & no
  Label1.Text = fullString
Member Avatar for Unhnd_Exception

Same as above but it won't matter how many zeros there are. If you run out of zeros it keeps going.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim IncrementedString As String = "ECJ-000"

        For i = 1 To 10000
            IncrementedString = IncrementString(IncrementedString, "-"c)
        Next

        MsgBox(IncrementedString)
    End Sub

    Public Function IncrementString(ByVal value As String, ByVal delimiter As Char) As String
        If value Is Nothing Then Return String.Empty

        Dim SplitString As String() = value.Split(delimiter)

        If SplitString.Length > 1 AndAlso IsNumeric(SplitString(1)) Then

            Dim Number As Integer = CInt(SplitString(1)) + 1
            Return SplitString(0) & _
                   delimiter & _
                   Number.ToString(New String("0"c, SplitString(1).Length))

        End If

        Return String.Empty

    End Function

Typically when you want to format an integer with leading zeroes the field width is known beforehand. In that case you can hardcode the width as

mynum.ToString("00000")

which will give you a five digit integer with leading zeroes. More generally you can code

mynum.ToString(StrDup(width,"0"))

where "width" can vary at runtime.

Member Avatar for Unhnd_Exception

Typically when you code with hard coded values you have to go back and change your code when you need to change the number of zeros. In that case you just waste time.

New String("0"c,width) should be used instead of StrDup(width,"0").

Based on your reasoning (avoiding hard coded width), can you please explain why you recommend against

mynum.ToString(StrDup(width,"0"))

which does not have a hard coded width? Is there a reason that

New String("0"c,width)

is preferable to

mynum.ToString(StrDup(width,"0"))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.