VB.NET Randome Function to replace values - vb.net

I have a Random Function in vb.net like this which returns the 6-digit alphanumeric string like this.
Public Function rand() As String
Dim rng As Random = New Random
Dim sb As New StringBuilder
' Selection of pure numbers sequence or mixed one
Dim pureNumbers = rng.Next(1, 11)
If pureNumbers < 7 Then
' Generate a sequence of only digits
Dim number As Integer = rng.Next(1, 1000000)
Dim digits As String = number.ToString("000000")
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, digits.Length)
sb.Append(digits.Substring(idx, 1))
Next
Else
' Generate a sequence of digits and letters
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, 36)
sb.Append(s.Substring(idx, 1))
Next
End If
Return sb.ToString()
End Function
The problem is that when it returns 0 or O or 1 or I and so on {"1", "I", "O", "0", "5", "S", "7"}
I want to replce these character with 'A' and then should return the final random string.

If I understand you correctly, you want something like this (untested):
dim exceptionLetters = _
{"1", "I", "O", "0", "5", "S", "7"}
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, 36)
Dim ltr = s.Substring(idx, 1)
if (exceptionLetters.Contains(ltr))
Return sb
End If
sb.Append(ltr);
Next

Related

How to end a for loop without a comma? VB.NET

i was about to save each number from one cell using for loop here is my code:
For Each node As XmlNode In xmlnode
Dim X As String
Dim Y As String
Dim Z As String
X = node.SelectSingleNode("X").InnerText
Y = node.SelectSingleNode("Y").InnerText
Z = node.SelectSingleNode("Z").InnerText
Dim noOfPts As Integer = 0 'number of data points in data
For i As Double = 0 To noOfPts
result = X + ","
result2 = Y + ","
Next
Next
the thing is i'm going to end the loop without saving the comma. Instead of this "1,2,3,4,5," it should be like this "1,2,3,4,5" remove the comma from the end of the loop.
With StringBuilder you can simplify the loop by removing if statements and make more efficient in most of the cases (especially when you building a string in the loop).
Dim xBuilder AS New StringBuilder()
Dim yBuilder As New StringBuilder()
Dim delimiter As String = ", "
For Each node As XmlNode In xmlnode
Dim X As String = node.SelectSingleNode("X").InnerText
Dim Y As String = node.SelectSingleNode("Y").InnerText
Dim Z As String = node.SelectSingleNode("Z").InnerText
For i As Integer = 0 To noOfPts
xBuilder.Append(X).Append(delimiter)
yBuilder.Append(Y).Append(delimiter)
Next
Next
' use Math.Max to prevent exception if builder is empty
xBuilder.Length = Math.Max(xBuilder.Length - delimiter.Length, 0)
yBuilder.Length = Math.Max(yBuilder.Length - delimiter.Length, 0)
Dim resultX As String = xBuilder.ToString() ' 1, 2, 3, 4, 5
Dim resultY As String = yBuilder.ToString()
We are removing last "redundant" delimiter by "cutting" stringbuilder's length by length of delimiter.
For using String.Join you need to create a collection of values first
Dim xValues AS New List<string>()
Dim yValues As New List<string>()
For Each node As XmlNode In xmlnode
Dim X As String = node.SelectSingleNode("X").InnerText
Dim Y As String = node.SelectSingleNode("Y").InnerText
Dim Z As String = node.SelectSingleNode("Z").InnerText
For i As Integer = 0 To noOfPts
xValues.Add(X)
xValues.Add(Y)
Next
Next
Dim resultX As String = String.Join(", ", xValues) ' 1, 2, 3, 4, 5
Dim resultY As String = String.Join(", ", yValues)
I like to do it this way:
For i As Double = 0 To noOfPts
If result <> "" Then result &= ", "
result &= X
If result2 <> "" Then result2 &= ", "
result2 &= Y
Next
Have fun!

vb.net arithmetic operation resulted in an overflow error

I'm trying to turn every value of an eight digit string, called "d8", into its ASCII value so then I have 8 integers. I then want to add them together to form one integer and display this value into OffFac.Text. But it always displays this error "Arithmetic operation resulted in an overflow".
Dim d8 As String
Dim Step2 As Integer
d8 = DEight.Text
Dim RandomNumber2C As Char = d8.Substring(0, 1)
Dim RandomNumber22C As Char = d8.Substring(1, 1)
Dim RandomNumber32C As Char = d8.Substring(2, 1)
Dim RandomNumber42C As Char = d8.Substring(3, 1)
Dim RandomNumber52C As Char = d8.Substring(4, 1)
Dim RandomNumber62C As Char = d8.Substring(5, 1)
Dim RandomNumber72C As Char = d8.Substring(6, 1)
Dim RandomNumber82C As Char = d8.Substring(7, 1)
Dim RandomNumberX As String = Asc(Mid(RandomNumber2C, 1))
Dim RandomNumber2X As String = Asc(Mid(RandomNumber22C,1))
Dim RandomNumber3X As String = Asc(Mid(RandomNumber32C, 1))
Dim RandomNumber4X As String = Asc(Mid(RandomNumber42C, 1))
Dim RandomNumber5X As String = Asc(Mid(RandomNumber52C, 1))
Dim RandomNumber6X As String = Asc(Mid(RandomNumber62C, 1))
Dim RandomNumber7X As String = Asc(Mid(RandomNumber72C, 1))
Dim RandomNumber8X As String = Asc(Mid(RandomNumber82C, 1))
Dim RandomNumberXX As String = CLng(RandomNumberX)
Dim RandomNumber2XX As String = CLng(RandomNumber2X)
Dim RandomNumber3XX As String = CLng(RandomNumber3X)
Dim RandomNumber4XX As String = CLng(RandomNumber4X)
Dim RandomNumber5XX As String = CLng(RandomNumber5X)
Dim RandomNumber6XX As String = CLng(RandomNumber6X)
Dim RandomNumber7XX As String = CLng(RandomNumber7X)
Dim RandomNumber8XX As String = CLng(RandomNumber8X)
Step2 = RandomNumberXX + RandomNumber2XX + RandomNumber3XX + RandomNumber4XX + RandomNumber5XX + RandomNumber6XX + RandomNumber7XX + RandomNumber8XX
Step2 = OffFac.Text
You are trying to concatenate (append a series of strings to one another) a series of numbers that are the ascii values of the numeric characters entered into your text box.
With the string "12345678" you get the following.
"49" + "50" + "51" + "52" + "53" + "54" + "55" + "56" = "4950515253545556"
The number 4950515253545556 is too large to be assigned to an integer.
Also, your last assignment statement should be
OffFac.text = step2
You probably want to do something like this
Dim RandomNumberX As Integer = Asc(Mid(RandomNumber2C, 1))
Dim RandomNumber2X As Integer = Asc(Mid(RandomNumber22C, 1))
Dim RandomNumber3X As Integer = Asc(Mid(RandomNumber32C, 1))
Dim RandomNumber4X As Integer = Asc(Mid(RandomNumber42C, 1))
Dim RandomNumber5X As Integer = Asc(Mid(RandomNumber52C, 1))
Dim RandomNumber6X As Integer = Asc(Mid(RandomNumber62C, 1))
Dim RandomNumber7X As Integer = Asc(Mid(RandomNumber72C, 1))
Dim RandomNumber8X As Integer = Asc(Mid(RandomNumber82C, 1))
Dim RandomNumberXX As Integer = CLng(RandomNumberX)
Dim RandomNumber2XX As Integer = CLng(RandomNumber2X)
Dim RandomNumber3XX As Integer = CLng(RandomNumber3X)
Dim RandomNumber4XX As Integer = CLng(RandomNumber4X)
Dim RandomNumber5XX As Integer = CLng(RandomNumber5X)
Dim RandomNumber6XX As Integer = CLng(RandomNumber6X)
Dim RandomNumber7XX As Integer = CLng(RandomNumber7X)
Dim RandomNumber8XX As Integer = CLng(RandomNumber8X)

Vowel Counter Broken in VB

I am struggling to get this vowel counter to work. I should be able to input the file ( am I doing the inputting right) should read the text document(which it doesn't) and then count the vowels. Also, if I could I could get it to count the number of words but this isn't necessary.
The error code say that the variable myline is already declared in the current block. ?? Thanks in advance.
Here is the code:
Imports System.IO
Module Module1
Sub Main()
Dim vowel As Integer = 0
Dim Text, myline As String
Dim objStreamReader As StreamReader
objStreamReader = New StreamReader("H:\vowelcounter.txt")
Dim myline = objStreamReader
Text = objStreamReader.ReadLine()
Do While Not Text Is Nothing
Console.WriteLine(text)
For x = 0 To Text.Length
If x = "a" Then
vowel = vowel + 1
End If
If x = "e" Then
vowel = vowel + 1
End If
If x = "i" Then
vowel = vowel + 1
End If
If x = "o" Then
vowel = vowel + 1
End If
If x = "u" Then
vowel = vowel + 1
End If
If x = "A" Then
vowel = vowel + 1
End If
If x = "E" Then
vowel = vowel + 1
End If
If x = "I" Then
vowel = vowel + 1
End If
If x = "O" Then
vowel = vowel + 1
End If
If x = "U" Then
vowel = vowel + 1
End If
Next
Loop
Console.ReadLine()
End Sub
End Module
The error code say that the variable myline is already declared in the
current block.
Yes, it is:
Dim Text, myline As String ' <----- HERE
Dim objStreamReader As StreamReader
objStreamReader = New StreamReader("H:\vowelcounter.txt")
Dim myline = objStreamReader ' <----- and HERE
So rename the first string variable or the StreamReader (why do you need two for one at all?).
You could achieve the vowel-counter much more easier:
Dim text As String = System.IO.File.ReadAllText("H:\vowelcounter.txt")
Dim vowels = From c In text Where "aeiouAEIOU".Contains(c)
Dim vowelCount As Int32 = vowels.Count()
You can get the word-count in this way:
Dim words = text.Split()
Dim wordCount As Int32 = words.Length
This assumes only spaces, tabs or new-line characters are delimiter. If you need other characters as well:
Dim wordDelimiter As Char() = {" "C, ControlChars.Tab, ","C, "."C, "!"C, "?"C, _
";"C, ":"C, "/"C, "\"C, "["C, "]"C, _
"("C, ")"C, "<"C, ">"C, "#"C, """"C, _
"'"C}
Dim words = text.Split(wordDelimiter, StringSplitOptions.None)

How can I get String values rather than integer

How To get StartString And EndString
Dim startNumber As Integer
Dim endNumber As Integer
Dim i As Integer
startNumber = 1
endNumber = 4
For i = startNumber To endNumber
MsgBox(i)
Next i
Output: 1,2,3,4
I want mo make this like sample: startString AAA endString AAD
and the output is AAA, AAB, AAC, AAD
This is a simple function that should be easy to understand and use. Every time you call it, it just increments the string by one value. Just be careful to check the values in the text boxes or you can have an endless loop on your hands.
Function AddOneChar(Str As String) As String
AddOneChar = ""
Str = StrReverse(Str)
Dim CharSet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim Done As Boolean = False
For Each Ltr In Str
If Not Done Then
If InStr(CharSet, Ltr) = CharSet.Length Then
Ltr = CharSet(0)
Else
Ltr = CharSet(InStr(CharSet, Ltr))
Done = True
End If
End If
AddOneChar = Ltr & AddOneChar
Next
If Not Done Then
AddOneChar = CharSet(0) & AddOneChar
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim S = TextBox1.Text
Do Until S = TextBox2.Text
S = AddOneChar(S)
MsgBox(S)
Loop
End Sub
This works as a way to all the codes given an arbitrary alphabet:
Public Function Generate(starting As String, ending As String, alphabet As String) As IEnumerable(Of String)
Dim increment As Func(Of String, String) = _
Function(x)
Dim f As Func(Of IEnumerable(Of Char), IEnumerable(Of Char)) = Nothing
f = _
Function(cs)
If cs.Any() Then
Dim first = cs.First()
Dim rest = cs.Skip(1)
If first = alphabet.Last() Then
rest = f(rest)
first = alphabet(0)
Else
first = alphabet(alphabet.IndexOf(first) + 1)
End If
Return Enumerable.Repeat(first, 1).Concat(rest)
Else
Return Enumerable.Empty(Of Char)()
End If
End Function
Return New String(f(x.ToCharArray().Reverse()).Reverse().ToArray())
End Function
Dim results = New List(Of String)
Dim text = starting
While True
results.Add(text)
If text = ending Then
Exit While
End If
text = increment(text)
End While
Return results
End Function
I used it like this to produce the required result:
Dim alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim results = Generate("S30AB", "S30B1", alphabet)
This gave me 63 values:
S30AB
S30AC
...
S30BY
S30BZ
S30B0
S30B1
It should now be very easy to modify the alphabet as needed and to use the results.
One option would be to put those String values into an array and then use i as an index into that array to get one element each iteration. If you do that though, keep in mind that array indexes start at 0.
You can also use a For Each loop to access each element of the array without the need for an index.
if the default first two string value of your output is AA.
You can have a case or if-else conditioning statement :
and then set 1 == A 2 == B...
the just add or concatenate your default two string and result string of your case.
I have tried to understand that you are looking for a series using range between 2 textboxes. Here is the code which will take the series and will give the output as required.
Dim startingStr As String = Mid(TextBox1.Text, TextBox1.Text.Length, 1)
Dim endStr As String = Mid(TextBox2.Text, TextBox2.Text.Length, 1)
Dim outputstr As String = String.Empty
Dim startNumber As Integer
Dim endNumber As Integer
startNumber = Asc(startingStr)
endNumber = Asc(endStr)
Dim TempStr As String = Mid(TextBox1.Text, 1, TextBox1.Text.Length - 1)
Dim i As Integer
For i = startNumber To endNumber
outputstr = outputstr + ", " + TempStr + Chr(i)
Next i
MsgBox(outputstr)
The First two lines will take out the Last Character of the String in the text box.
So in your case it will get A and D respectively
Then outputstr to create the series which we will use in the loop
StartNumber and EndNumber will be give the Ascii values for the character we fetched.
TempStr to Store the string which is left off of the series string like in our case AAA - AAD Tempstr will have AA
then the simple loop to get all the items fixed and show
in your case to achive goal you may do something like this
Dim S() As String = {"AAA", "AAB", "AAC", "AAD"}
For Each el In S
MsgBox(el.ToString)
Next
FIX FOR PREVIOUS ISSUE
Dim s1 As String = "AAA"
Dim s2 As String = "AAZ"
Dim Last As String = s1.Last
Dim LastS2 As String = s2.Last
Dim StartBase As String = s1.Substring(0, 2)
Dim result As String = String.Empty
For I As Integer = Asc(s1.Last) To Asc(s2.Last)
Dim zz As String = StartBase & Chr(I)
result += zz & vbCrLf
zz = Nothing
MsgBox(result)
Next
**UPDATE CODE VERSION**
Dim BARCODEBASE As String = "SBA0021"
Dim BarCode1 As String = "SBA0021AA1"
Dim BarCode2 As String = "SBA0021CD9"
'return AA1
Dim FirstBarCodeSuffix As String = Replace(BarCode1, BARCODEBASE, "")
'return CD9
Dim SecondBarCodeSuffix As String = Replace(BarCode2, BARCODEBASE, "")
Dim InternalSecondBarCodeSuffix = SecondBarCodeSuffix.Substring(1, 1)
Dim IsTaskCompleted As Boolean = False
For First As Integer = Asc(FirstBarCodeSuffix.First) To Asc(SecondBarCodeSuffix)
If IsTaskCompleted = True Then Exit For
For Second As Integer = Asc(FirstBarCodeSuffix.First) To Asc(InternalSecondBarCodeSuffix)
For Third As Integer = 1 To 9
Dim tmp = Chr(First) & Chr(Second) & Third
Console.WriteLine(BARCODEBASE & tmp)
If tmp = SecondBarCodeSuffix Then
IsTaskCompleted = True
End If
Next
Next
Next
Console.WriteLine("Completed")
Console.Read()
Take a look into this check it and let me know if it can help

Generate "aa", "Aa", "aA", "AA", etc In order

How would I generate combinations of 2 lowercase/upercase letters/numbers in order?
Sub Main()
Dim Looper As Boolean = True
While Looper = True
'String = "aa", "Aa", "aA", "AA"
'WebClient.DownloadString("link.com/" & String")
End While
End Sub
Like this, but generate combination for String
You could loop over a string and just manipulate upper and lower case as required:
Sub Main()
Dim results as List(of String) = new List(of String)()
For Each c as Char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
results.Add(c.ToString().ToLower() & c.ToString().ToLower())
results.Add(c.ToString() & c.ToString().ToLower())
results.Add(c.ToString().ToLower() & c.ToString())
results.Add(c.ToString() & c.ToString())
Next
End Sub
This will generate a combination of two random characters, including numbers and uppercase/lowercase letters:
Public Function GetRandomString(ByVal iLength As Integer) As String
Dim sResult As String = ""
Dim rdm As New Random()
For i As Integer = 1 To iLength
sResult &= ChrW(rdm.Next(32, 126))
Next
Return sResult
End Function
Or you can do the common random string defining the valid caracters:
Public Function GenerateRandomString(ByRef iLength As Integer) As String
Dim rdm As New Random()
Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray()
Dim sResult As String = ""
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function