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
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
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?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
0
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
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.