Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Looping and Arrays

  1. #1

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Resolved [RESOLVED] [2005] Looping and Arrays

    Hello,

    I am stuck in trying to figure out how to add values to the parameters while looping. The code below is hard-coded to add the value of the array to the parameter which works fine if there is only 1 line to import but I know for a fact that the files will be more than 1 line long. Please help!

    Code:
        If varType = "LogCurve" Then
                        For i = 0 To (UBound(results.ToArray())) / 3
                           
                            If chkApi.Checked = True Then
                                varApi = splitout2(x + (nbxApi.Value - 1))
                               Else
                                varApi = txtApi.Text
                                
                            End If
                            
                            If nbxDepth.Value = 0D Then
                                varDepth = 0
                            Else
                                If results(x + (nbxDepth.Value - 1)) = "" Then
                                    varDepth = 0
                                Else
                                    varDepth = results(0)
    
                                End If
                            
                              
                                If nbxSPC.Value = 0D Then
                                    varSPC = 0
                                Else
                                    If results(x + (nbxSPC.Value - 1)) = "" Then
                                        varSPC = 0
                                    Else
    
                                        varSPC = results(1)
                                    End If
                                End If
                                
    
                                If nbxVSH.Value = 0D Then
                                    varVSH = 0
                                Else
                                    If results(x + (nbxVSH.Value - 1)) = "" Then
                                        varVSH = 0
                                    Else
                                        varVSH = results(2)
                                    End If
                                End If
                             
                                Me.InsertDB()
                               
                                varCount = varCount + 1
                            End If
    
                        Next
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Looping and Arrays

    Not quite sure what you are asking for, but at the very least, this is suspicious:

    vb Code:
    1. If results(x + (nbxDepth.Value - 1)) = "" Then
    2.                                 varDepth = 0
    3.                             Else
    4.                                 varDepth = results(0)
    5.  
    6.                             End If

    If results holds a string, then the only way varDepth = results(0) will work is if you have Option Strict Off, which you really shouldn't do.

    Other than that, what is results? How is it defined? Is that even the array you are talking about (sure looks like one)?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Looping and Arrays

    What I am doing is importing a csv file and storing the data in an array. I create a list of strings, named results, and add all the values of the array to it minus all the null values. Results stores all the values from which were imported from the csv file minus the null values. I use .ToArray to make results an array. I then loop to assign the values from the array to the variables varDepth, varSPC, varVSH. This is the part where I am getting stuck at. I do not know how to add the value from the array to the variable in my program. The way it is written now is varDepth = results(0). This works but only if their are only 3 items in the array. The csv file will have more than just 3 items.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Looping and Arrays

    There is a difficult problem in here. If there is no pattern to which values are null in the CSV rows, how will you know where the items are located in the array? It seems to me that if the CSV has the same number of fields, and some of them are simply empty, then you wouldn't exclude the empty ones such that item X is always the Nth item in a row, and hence the Nth item in the array. Perhaps that's not an issue?

    The other problem is that there is probably no good way to do this. If you are taking the values from an array of some variable size, and assigning those values to individual variables, then you have to have as many variables as you have slots in the array. In short: You need an array of variables.

    You would have to do exactly what you currently are doing, just repeat it as many times as is necessary for all the variables. Alternatively, you could just use an array of a different type, and convert the strings from Results into the other array. This could be done in a loop, because the index of each slot in the array would be the same for both arrays. The only alternative is to do exactly what you are doing but add enough If blocks for as many potential values as you might possibly have in Results. That would be ugly, and it is hard to say how it would be of any use.
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width