 # vb.net running number auto increases fit digit to alphabet [Answered]RSS

## 3 replies

Last post Sep 18, 2018 09:53 AM by vahid bakkhi

• ### vb.net running number auto increases fit digit to alphabet

Sep 18, 2018 06:34 AM|FookWah|LINK

I going to create an application to generate a number increment by one to always fix 4 digit.

which are from number 0 to 9999 after +1

when 10001, it become A000

when A999 +1 it become B000

B999 +1 = C000

IS that vb.net got any library to complete my application

Below method when hit 10000 it will not change to A000

``````Dim a As Integer = 5 + 1
Dim b As String = Format(a, "0000")
Label1.Text = b``````
• ### Re: vb.net running number auto increases fit digit to alphabet

Sep 18, 2018 07:48 AM|vahid bakkhi|LINK

hi

you can write a function below like :

```Public Function GenerateNumber(ByVal num As String) As String
Dim chars = {"A", "B", "C", "D", "E", "F"}
Dim shouldAddalpha As Boolean

If (num.Length <> 4) Then
Throw New Exception("invalid format")
End If

Dim firstDigit = num.Substring(0, 1)
Dim secondDigit = Convert.ToInt32(num.Substring(1, num.Length - 1))

If (secondDigit = 999) Then
secondDigit = 0
shouldAddalpha = True
End If

If (shouldAddalpha) Then
Dim query = chars.Select(Function(o, i) New With {.Widget = o, .Index = i}) _
.FirstOrDefault(Function(s) s.Widget = firstDigit)

If query Is Nothing Then
firstDigit = "A"
Else
firstDigit = chars(query.Index + 1)
End If
Else
secondDigit = secondDigit + 1
End If

Return firstDigit +secondDigit.ToString("D3")

End Function```

and use below like :

``` Dim result = GenerateNumber("A998")
result = GenerateNumber(result)
result = GenerateNumber(result )```

result

```A999
B000
B001```

vahid bakhtiary

Please MARK AS ANSWER if suggestion helps.
• ### Re: vb.net running number auto increases fit digit to alphabet

Sep 18, 2018 09:40 AM|FookWah|LINK

Thanks you Vahid Bakhtiary. It work like what I'm expected.

Thanks you very much for helping.

```Dim query = chars.Select(Function(o, i) New With {.Widget = o, .Index = i}) _
.FirstOrDefault(Function(s) s.Widget = firstDigit)```

May I know what is this LINQ foreach perform.

• ### Re: vb.net running number auto increases fit digit to alphabet

Sep 18, 2018 09:53 AM|vahid bakkhi|LINK

first I created an array for your character such as A,B,C,D,E,...

for example, your  number is A001

in the LINQ query ,   check A is in the above array, if yes then return the index of character into the array. for example for this sample will be 0

Finally get the next index of the array the be 1, the index of 1 stand for B

vahid bakhtiary

Please MARK AS ANSWER if suggestion helps.