creating a counter with a for each loop

I need to pass a counter as an argument to a function within a for each loop in vb.net.

my loop:

For Each line In inFileContents.Split(arg, StringSplitOptions.None)
    Dim objFtpFileLine As New FtpFileLineClass()
    outStatusString = Upload_ToDatabase(FileId, LineNumer, line)
Next


The counter will be assigned to the variable LineNumber.

Any help would be nice.

Thank you.
metropiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brendanmeyerCommented:
Dim Line = inFileContents.Split(arg, StringSplitOptions.None)
For Int32 LineNumer = 0 To Line.Count
    Dim objFtpFileLine As New FtpFileLineClass()
    outStatusString = Upload_ToDatabase(FileId, LineNumer + 1, Line[LineNumer])
Next

Open in new window


I think this is the code you want to use

Sorry if there are syntax errors, I haven't used VB.Net in a long time
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
That's pretty close...more like:
        Dim lines() As String = inFileContents.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        For i As Integer = 0 To lines.Length - 1
            Dim objFtpFileLine As New FtpFileLineClass()
            outStatusString = objFtpFileLine.Upload_ToDatabase(FileId, i, lines(i))
        Next

Open in new window

Mel-Leo RosalCommented:
        Dim linecount As Integer
        Dim Line() As String = inFileContents.Split(vbCrLf)
        For x As Integer = 0 To UBound(Line) - 1
            linecount = x + 1

            Dim objFtpFileLine As New FtpFileLineClass()
            outStatusString = Upload_ToDatabase(FileId, linecount, Line(x))
        Next

Open in new window


Something like this?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

metropiaAuthor Commented:
Thank you all for your help.

One small question.

I see that the first line to be read is zero element, how can that be altered, so that the first line read is 1?

Thank you
metropiaAuthor Commented:
would it be to change:  For LineNumber As Integer = 0 To lines.Length - 1

To:  For LineNumber As Integer = 1 To lines.Length - 1
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It will always be zero as .Net is a zero based language.

Just add one to your index in your code.  Leave the loop alone, though, as you'll get an index out of range error, or skip the first line.

So you'd do something like:
    For LineNumber As Integer = 0 To lines.Length - 1 ' <-- ZERO here
        ' Add ONE in here for a One based index for display purposes, but still use LineNumber as is for access:
       Debug.Print("Line #" & (LineNumber + 1) " = " & lines(LineNumber))
    Next

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.