Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:40:15+00:00 2026-06-05T05:40:15+00:00

I’m trying to make a function in VB.net that will loop through an algorithm.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:40:16+00:00Added an answer on June 5, 2026 at 5:40 am

    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)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.