Click to See Complete Forum and Search --> : Incrementing a Number in a Char


drone11
03-28-2008, 10:25 PM
I was wondering if someone can help me out on this. Is there a better way to do this. I have a UserId that consit of a varchar(6). Lets say the first member is A00001. Yet when I try to create a new member on an asp page I want the next available spot which would be A00002. The problem is that I dont want to use for example (A & "0000" & Num) to get A00003. Because at the end if I get to the 10th memberID it would come out with an extra 0 or would it pop out an error message. I want it to come out A00010 and for A00999 to turn to A01000 and so forth. I am new to programming so bare with me. Thanx.

Here is what I got:

Me.lblNumMiembro.Text = dt.Rows.Item(dt.Rows.Count - 1).Item(0) //Here I would get "A00009"

Now all I need is a method that divides this last string ("A00009") acquired from a stored procedure

A method that divides it into (A + 000 +09). Then with this method add 1 more which will be A + 000 + 10)

Which in the end returns this number "A00010" which I will later assign it to Me.lblNumMiembro.Text= newNum

Can anyone show me a method or vbcode that does this. I mean the code behind dividing the string and turning it into an integer and finally incrementing it by 1 to have the result of A00010. Then when I ask it again it will display A00011

chazzy
03-29-2008, 12:30 PM
I would say to not keep the different values separate. Assuming you're using some kind of db, or even a simple data structure, where you have a string containing the prefix, "A", and an int containing the sequence value (9,10, etc), then use pad left to make up the size difference..


Dim str1 As String = "A"
Dim seq As String = "10"
`seq would be the string version of the int value
Console.WriteLine(str1+seq.PadLeft(4, "0"))


Not overly tested, but you get the idea. 4 would be dynamically calculated based on the length of seq

aliz
04-16-2008, 01:11 AM
Hi,
i m not an expert in coding..
but i think u should use single quote instead of double with zero...
otherwise it will generate an error...
am i right?

chazzy
04-16-2008, 07:12 AM
in this case, we're treating 0 as a string, so double quotes apply.