How to Create a Random ID Generator in Visual Basic

Language

Introduction: Welcome to a tutorial on how to create a Random ID generator. Steps of Creation: Step 1: First lets import System.Net and System.Text so we can use the internet and a function get a string between two points within a given string. Let's import those now and create our getbetween function...
  1. Imports System.Net
  2. Imports System.Text.RegularExpressions
  3.  
  4. Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
  5. Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
  6. End Function
Step 2: Now, on a button click event, lets get the response from fakenamegenerator.com (which will provide us with the details) and then extract the information. I have chosen to only extract; Name Address Phone Email However, you can choose to add more. Once we have extracted all the information we output it in a messagebox line by line. We also surround the whole thing in a Try and Catch statement just in case the website returns a bad response.
  1. Try
  2. Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.fakenamegenerator.com/")
  3. r.KeepAlive = True
  4. r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  5. Dim re As HttpWebResponse = r.GetResponse()
  6. Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
  7. Dim nameSpare As String = GetBetween(src, "<div class=""address"">", "</div>")
  8. Dim name As String = GetBetween(nameSpare, "<h3>", "</h3>")
  9. Dim address As String = GetBetween(nameSpare, "<div class=""adr"">", "</div>")
  10. address = address.Replace(" ", "")
  11. address = address.Replace("<br/>", ", ")
  12. address = address.Replace("</br>", ", ")
  13. Dim extraSpace As String = GetBetween(src, "<div class=""extra"">", "</div>")
  14. Dim phone As String = GetBetween(extraSpace, "<li class=""tel""><span class=""value"">", "</span></li>")
  15. Dim email As String = GetBetween(extraSpace, "<li class=""email""><span class=""value"">", "</span>")
  16. MsgBox(name & vbNewLine & address & vbNewLine & phone & vbNewLine & email)
  17. Catch ex As Exception
  18. MsgBox(ex.ToString())
  19. End Try
Project Complete! That's it! Below is the full source code and download to the project files.
  1. Imports System.Net
  2. Imports System.Text.RegularExpressions
  3.  
  4. Public Class Form1
  5. Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
  6. Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
  7. End Function
  8. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  9. Try
  10. Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.fakenamegenerator.com/")
  11. r.KeepAlive = True
  12. r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  13. Dim re As HttpWebResponse = r.GetResponse()
  14. Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
  15. Dim nameSpare As String = GetBetween(src, "<div class=""address"">", "</div>")
  16. Dim name As String = GetBetween(nameSpare, "<h3>", "</h3>")
  17. Dim address As String = GetBetween(nameSpare, "<div class=""adr"">", "</div>")
  18. address = address.Replace(" ", "")
  19. address = address.Replace("<br/>", ", ")
  20. address = address.Replace("</br>", ", ")
  21. Dim extraSpace As String = GetBetween(src, "<div class=""extra"">", "</div>")
  22. Dim phone As String = GetBetween(extraSpace, "<li class=""tel""><span class=""value"">", "</span></li>")
  23. Dim email As String = GetBetween(extraSpace, "<li class=""email""><span class=""value"">", "</span>")
  24. MsgBox(name & vbNewLine & address & vbNewLine & phone & vbNewLine & email)
  25. Catch ex As Exception
  26. MsgBox(ex.ToString())
  27. End Try
  28. End Sub
  29. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment