I'm trying to make a function in VB.net that will loop through an algorithm. I've split the algorithm into an array using the Split command, so I have an array with the values.
I then try to loop through them and replace a # with "Number" where necessary, however VB.net throws an error.
I'm fairly new to VB.net, so I'm unsure why it's doing this.
Algorithms are in the format A B C D E F 1 2 3 #
Function generate(ByVal alg As String)
Dim algSplit As String() = alg.Split(" ")
For Each digit In algSplit
Dim replacement As String = algSplit(digit).Replace("#", "Number")
algSplit(digit) = replacement
Next
Dim result As String = String.Join("", algSplit)
MsgBox(result)
End Function
Is there a quick fix?
Question Part 2: I got the loop working; however I broke part of the functionality I was aiming for.
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Dim Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max)
End Function
Public Function RandLet() As String
Dim number As Integer = GetRandom(1, 26)
Dim Alphabet() As String = New String() {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V""W", "X", "Y", "Z"}
Dim Letter As String = Alphabet(number)
Return Letter
End Function
Function generate(ByVal alg As String) As String
Dim algSplit As String() = alg.Split(" "c)
For index As Int32 = 0 To algSplit.Length - 1
algSplit(index) = algSplit(index).Replace("#"c, GetRandom(1, 9)).Replace("%"c, RandLet())
Next
Dim result As String = String.Join("", algSplit)
MsgBox(result)
Return result
End Function
End result from A B C D E F 1 2 3 | L % % % | N # # #
ends up as ABCDEF123|LXXX|N888
Essentially, each # and each % are being replaced by the same number instead of a different one each time. I thought that being in a loop this wouldn't happen, what have I missed?