1

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?

SatansFate
  • 47
  • 1
  • 1
  • 6

4 Answers4

2

You are For-Eaching with a string variable since digit is finding "A", which is a string, but you are using it as an index: algSplit(digit)..

You can try this instead:

For i As Integer = 0 To algSplit.Length - 1
  algSplit(i) = algSplit(i).Replace("#", "Number")
Next

If you are just looking at replacing the # and the empty spaces, this works, too:

alg = alg.Replace(" ", String.Empty).Replace("#", "Number")
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • This is good, but I was eventually going to replace the # with a random number for each one so I wanted a loop to make sure each was randomised separately. – SatansFate Jun 01 '12 at 12:41
1

That's probably easier with LINQ:

Dim value = "A B C D E F 1 2 3 #"
Dim replaced = From digit In value.Split(" "c)
             Select digit.Replace("#"c, "Number")
Dim result = String.Join("", replaced)

Your "traditional" approach, corrected:

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, "Number")
    Next
    Dim result As String = String.Join("", algSplit)
    Return result
End Function
  1. Methods(Functions in VB) must return something. Your function doesn't return anything, hence add AS String at the end.
  2. Split has several overloads, the one you're using takes a Char instead of string, so replace alg.Split(" ") with alg.Split(" "c)
  3. If you want to replace an array value with something other, it's better to you a For-Loop instead of a For-Each since you need to use the index often (see above)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I came across another issue using this; so I updated the original question. – SatansFate Jun 01 '12 at 13:06
  • 1
    @SatansFate: Pass the random instance to the method since it's inititialized with the current time. That'll produce always the same "random" value when called in a short time. – Tim Schmelter Jun 01 '12 at 13:10
  • I don't fully understand what you mean - could you elaborate? – SatansFate Jun 01 '12 at 13:39
  • @SatansFate: There are many similar questions on SO, try this one: http://stackoverflow.com/questions/767999/random-number-generator-not-working-the-way-i-had-planned-c – Tim Schmelter Jun 01 '12 at 14:33
1

You are looping on an array of strings.
The foreach on algSplit return a string not an integer.
You can't use that digit string as an index into the array

Replace your code with this:

Function generate(ByVal alg As String)     
    Dim algSplit As String() = alg.Split(" ")     
    For digit = 0 to algSplit.Length - 1
        Dim replacement As String = algSplit(digit).Replace("#", "Number")         
        algSplit(digit) = replacement     
               ' could be simply written as 
               ' algSplit(digit) = algSplit(digit).Replace("#", "Number")     

    Next     
    Dim result As String = String.Join("", algSplit)     
    ??? .. you should return something here ????
End Function 
Steve
  • 213,761
  • 22
  • 232
  • 286
-2
Function generate(ByVal alg As String)     
    Dim algSplit As String() = alg.Split(" ")     
    For digit = 0 to algSplit.Length - 1
        Dim replacement As String = algSplit(digit).Replace("#", "Number")         
        algSplit(digit) = replacement     
               ' could be simply written as 
               ' algSplit(digit) = algSplit(digit).Replace("#", "Number")     

    Next     
    Dim result As String = String.Join("", algSplit)     
    ??? .. you should return something here ????
End Function
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Dung
  • 1