for each loop only grabbing the last value of the string array

Viewed 53

Note: I'm not posting all code due to it's over 500 lines, I'll show a summary of what I'm trying to accomplish and the issue:

I have a string array that looks like this: New_BMWM3889;New_LEXIS600;789858;Used_VOL9998

I need to call a routine (the same routine) that will add formatting to the value. I've tried a for each loop, but it's only grabbing the last value of the string array.

I've tried something like this:

dim cars as String = "New_BMWM3889;New_LEXIS600;789858;Used_VOL9998"
dim tmp as String() = cars.Split(";")
dim vin as String

For Each c in tmp
    If p.Conatains("New") Then
        vin = FormatVin("New", "@", newFormat('0000'))  
    Else
        vin = FormatVin("No Model", "&", newFormat('####'), 
    End If 
Next

so, I have to call the same routine and pass different parameters to the FormatVin routine, however, when I run this I'm only getting the last value of the string array. The formatVin does format validation and will change the format if needed, but that's not the issue, how can I call that same routine but pass different parameters based on if the string in the string array has a prefix or not? Then once the formatting is completed, all of the new formatted values will be passed into a String builder to be used to pass to SQL,

so,

  1. Need to grab all values from the string array
  2. call the routine with the correct parameters based off of the string value.
  3. take all the new formatted strings and passed as one string into a new routine that builds a SQL statement. I know it's a mess, and I'm not sure if it can really be done cleanly if at all. So at the end I should have so I can pass this into my where clause

New_BMWM3889000;New_LEXIS600000;000000789858;Used_VOL9998000

1 Answers

It's hard to say exactly but I think that you should be using something along these lines:

Dim cars = "New_BMWM3889;New_LEXIS600;789858;Used_VOL9998"
Dim tmp = cars.Split(";"c)

For i = 0 To tmp.GetUpperBound(0)
    Dim vin = tmp(i)

    If vin.Conatains("New") Then
        vin = FormatVin("New", vin, newFormat('0000'))  
    Else
        vin = FormatVin("No Model", vin, newFormat('####'), 
    End If

    tmp(i) = vin
Next

cars = String.Join(";", tmp)

It seems like you need to get data out of and into that array. You haven't told us what FormatVin does but I would also assume that you have to pass in the data from the array and get back out a modified version to put back into the array. I might be off on some of the detail, given your vague explanation, but I think this is the basic structure you need.

Related