PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How do I concatenate this string?-VBForums
Results 1 to 2 of 2

Thread: How do I concatenate this string?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2019
    Posts
    20

    How do I concatenate this string?

    this is inn my code. I prefer not to create 50 if statements

    Code:
       For x = 1 To 50
    
                    If x = 1 Then
                        For Each xx As Racer In allRacers
                                y.Items.Add(xx.Race1.Substring(0, xx.Race1.IndexOf(">")) & "-" & xx.Car)
                        Next
                    End If
    
                    If x = 2 Then
                        For Each xx As Racer In allRacers
                                y.Items.Add(xx.Race2.Substring(0, xx.Race2.IndexOf(">")) & "-" & xx.Car)
                        Next
                    End If
    
    ..... do this 50 more times
    Can I not just use "race" & x instead of race1 , race 2...
    something like list?

    Code:
    For Each xx As Racer In allRacers
          y.Items.Add(xx.Race & x & .Substring(0, xx.Race & x & .IndexOf(">")) & "-" & xx.Car)
    Next
    thanks for looking

    Ross

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,245

    Re: How do I concatenate this string?

    No you can't. An identifier is not a String. This:
    vb.net Code:
    1. Dim x = 1
    2. Dim result = xx.Race & x
    would not set result to the value of xx.Race1 but rather get the value of xx.Race and convert it to a String, then append "1" to it. Of course, if xx.Race doesn't exist then that's a syntax error.

    If you want to identify a number of values by a String then you have to have a means to do that, e.g. if you're talking about controls then you can index the Controls collection of their parent. That's obviously not what you're doing though. What you should do is create an array or collection containing your multiple values, then use your number to index that, e.g.
    vb.net Code:
    1. Dim races = {xx.Race1, xx.Race2, ...}
    2. Dim race = races(x)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width