looping and counting

bsc505

New member
Joined
Nov 20, 2009
Messages
2
Programming Experience
Beginner
So i am count after i find each string example when i find "ADT^A08" it counts how many found in the txt file and then it moves to the next file . My problem is that you can see totalA08 += 1 counts the amount. My problem is that it keeps its old value after each run so the first run cuonts 5000 and then the next count is 10000 . When that last file was the same as the first only 5000. Any ideas on how to properly clear that value after its done
thanks'


VB.NET:
mports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Imports System.Collections
Imports System.Data.SqlClient


Partial Public Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()
        Dim fileListing As New DirectoryInfo("\\New Folder\")
        Dim ifile As Integer
        Dim strfile As String
        For Each file As FileInfo In fileListing.GetFiles("*.*")
            lstBoxFiles.Items.Add(file)
        Next
        ifile = 0
        For ifile = ifile To lstBoxFiles.Items.Count - 1

                      strfile = "\\New Folder\" + lstBoxFiles.Items(ifile).ToString
            Dim r As New System.IO.StreamReader(strfile)
            While r.Peek <> -1
                Dim filecontents As String = String.Empty
                'add each line to the contents variable
                filecontents = r.ReadLine
                Dim A08count As String
                Dim totalA08minus As Integer
                Dim totalA08 As Integer
                Dim A01count As String
                Dim totalA01minus As Integer
                Dim totalA01 As Integer
                Dim BARcount As String
                Dim totalBAR As Integer
                Dim totalBARminus As Integer
                Dim MSHcount As String
                Dim totalMSH As Integer
                Dim totalMSHminus As Integer


                Dim subdate As String = filecontents
                Dim subdatevalue As String
                If filecontents.Contains(":2009") Then
                    subdate = subdate.Substring(subdate.IndexOf("2009") + 0, 8)
                    subdatevalue = subdate.ToString
                End If

                If filecontents.Contains("|ADT^A08|") Then

                    Dim foundindex As Integer = filecontents.ToString.IndexOf("|ADT^A08|")
                    A08count = filecontents.Substring(filecontents.IndexOf("|ADT^A08|"))
                    totalA08 += 1
                    totalA08minus = totalA08.ToString.Count - 1
                End If
                If filecontents.Contains("|ADT^A01|") Then

                    Dim foundindex As Integer = filecontents.ToString.IndexOf("|ADT^A01|")
                    A01count = filecontents.Substring(filecontents.IndexOf("|ADT^A01|"))
                    totalA01 += 1
                    totalA01minus = totalA01.ToString.Count - 1
                End If
                If filecontents.Contains("|BAR^P01|") Then

                    Dim foundindex As Integer = filecontents.ToString.IndexOf("|BAR^P01|")
                    BARcount = filecontents.Substring(filecontents.IndexOf("|BAR^P01|"))
                    totalBAR += 1
                    totalBARminus = totalBAR.ToString.Count - 1
                End If

                ' adds all the data that was read from the streamreader to richtextbox1
                Dim someText As String = filecontents
                Dim strLen As Integer = someText.Length
                Dim finished As Boolean = False

                If r.EndOfStream = True Then

                    Dim typeString As String
                    Try
                        typeString = ListBox1.Items.ToString()
                    Catch ex As Exception
                        MessageBox.Show("Nothing was found there dummy " + ex.Message + "")
                        Return
                    End Try

                    Dim SQLStringMSH As String = "
 
yeah i tryed that but it will just stay at 0 and never count , in other words when it goes through it will stay at 0 than 1, then 0 than 1, how do i get it to keep counting?
 
You're defining totalA08, and many other variables, within your While loop. If you want the value of a variable to be maintained throughout an entire loop then it has to be declare OUTSIDE the loop. If you declare a variable inside a loop then it ceases to exist at the end of each iteration and gets created anew at the beginning of the next.
 

Latest posts

Back
Top