Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,
I have a problem. I have some code which generates an API key. It needs one letter at the front and 19 numbers after it. The problem is my code won't convert the first number into a letter.
Here is my code (it is long and slow), if you can improve it, please feel free to do so:
VB
Private Function GenerateAPI() As String
        Dim letter As String
        Dim number As String
        Randomize()
        number = Int(Rnd() * 26) + 1
        letter = number.Replace(1, "a")
        letter = number.Replace(2, "b")
        letter = number.Replace(3, "c")
        letter = number.Replace(4, "d")
        letter = number.Replace(5, "e")
        letter = number.Replace(6, "f")
        letter = number.Replace(7, "g")
        letter = number.Replace(8, "h")
        letter = number.Replace(9, "i")
        letter = number.Replace(10, "j")
        letter = number.Replace(11, "k")
        letter = number.Replace(12, "l")
        letter = number.Replace(13, "m")
        letter = number.Replace(14, "n")
        letter = number.Replace(15, "o")
        letter = number.Replace(16, "p")
        letter = number.Replace(17, "q")
        letter = number.Replace(18, "r")
        letter = number.Replace(19, "s")
        letter = number.Replace(20, "t")
        letter = number.Replace(21, "u")
        letter = number.Replace(22, "v")
        letter = number.Replace(23, "w")
        letter = number.Replace(24, "x")
        letter = number.Replace(25, "y")
        letter = number.Replace(26, "z")
        GenerateAPI = letter
        MsgBox(letter)
        For i As Integer = 0 To 19
            Randomize()
            Dim x As Integer = Int(Rnd() * 9) + 1
            GenerateAPI &= x
        Next
    End Function



Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Mar-15 22:56pm    
This code sample is opposite to programming. You should not repeat anything as you do, repeating the same string 26 times. Use the loop. Use other abstractions. Programming is all about abstractions and not doing repetitive work.

Now, what you are trying to achieve? Anyway, your problem does not worth fixing (which is way too trivial), until you grasp what is programming and what is good for.

—SA
iProgramIt 19-Mar-15 23:03pm    
You are a very rude person. The same person who keeps FU**ing baggin me. Shut the hell up and piss off
Sergey Alexandrovich Kryukov 19-Mar-15 23:19pm    
No, I'm not, not at all, I only have the habit to tell truth, I never say anything bad about a person. The rude behavior here is yours, writing bad words which almost no one allows here, not only ignorant. It's up to you if you want to keep fooling yourself, it's only bad for you no one else. Sorry, really sorry for you. You cannot get any help.
—SA

Here is a quick and nasty solution... I have omitted the letters I and O since they can be easily confused for 1 and 0...

Just a couple of comments...

1. Strings are immutable, strings immutable has many advantages. It provides automatic thread safety, and makes strings behave like an intrinsic type in a simple, effective manner. This allows for extra efficiencies at runtime (such as allowing effective string interning to reduce resource usage), and has huge security advantages, since it's impossible for an third party API call to change your strings.

StringBuilder was added in order to address the one major disadvantage of immutable strings - runtime construction of immutable types causes a lot of GC pressure and is inherently slow. By making an explicit, mutable class to handle this, this issue is addressed without adding unneeded complication to the string class.

2. The ToCharArray() function will allow you to quickly break a string into a character array that you can use for your first character.

VB
Dim r As New Random()
Dim x = "abcdefghjklmnpqrstuvwxy"
Dim xx = x.ToCharArray()
Dim sb = New StringBuilder()
sb.Append(xx(r.[Next](0, xx.Length)))
For i As Integer = 0 To 18
    sb.Append(r.[Next](0, 10))
Next
Console.WriteLine(sb.ToString())
Console.ReadKey()


PS Please don't be so harsh... We all started somewhere...
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Mar-15 23:27pm    
Pretty good answer and good points, I voted 4.
The only problem is that we don't know the goal. Is the string "1" is supposed to be replaced with "a"? We cannot be so sure. Most likely, the solution is not yet adequate to the goal.
Also, you don't need to introduce the string "abcd...", because the code points of the alphabet goes in the alphabetical order, you just need a type cast.
—SA
LGM-Developer 20-Mar-15 0:01am    
I don't like confusing things like serial numbers with 1,0, I,O ... so I usually drop I and O from the list.

<pre lang="vb">

Dim r As New Random()
Dim sb = New StringBuilder()
sb.Append(CChar(r.[Next](97, 123)))
For i As Integer = 0 To 17
sb.Append(r.[Next](0, 10))
Next
Console.WriteLine(sb.ToString())
Console.ReadKey()

</pre>
[no name] 19-Mar-15 23:33pm    
Can't you access chars in a string via indexer so that ToCharArray could be skipped? (Innocent question, no VB experience here)
+4 from me too
Yes... you can...

That would simplify the code somewhat...

C#
Dim r As New Random()
Dim x = "abcdefghjklmnpqrstuvwxy"
Dim sb = New StringBuilder()
sb.Append(x(r.[Next](0, x.Length)))
For i As Integer = 0 To 18
	sb.Append(r.[Next](0, 10))
Next
Console.WriteLine(sb.ToString())
Console.ReadKey()
 
Share this answer
 

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