Results 1 to 4 of 4

Thread: VB 2010 Counter from reading a file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    8

    VB 2010 Counter from reading a file

    I am trying to make a loadcounter so that as a form loads an ID number is input into a readonly text box. This would be calculated from the hash address of the last saved record saved on the file plus 1000. I am stumped as to what to do. Any help?

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB 2010 Counter from reading a file

    Hello,

    I would suggest showing code you currently have, otherwise it's a pot-shot in which direction to go in for assisting you.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    8

    Re: VB 2010 Counter from reading a file

    'Zero the loadcounter
    Loadcounter = 0

    'Open the file to count customer data
    FileOpen(1, "CustomerDetails.dat", OpenMode.Random, , , Len(Customer))
    While Not EOF(1)
    FileGet(1, "CustomerDetails.dat")
    Loadcounter = Loadcounter + 1
    End While
    FileClose(1)

    'add 1 to create new customer ID number
    txtCustomerID.Text = (1000 + Loadcounter + 1)

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB 2010 Counter from reading a file

    Okay, I am not versed in VB6 style of coding so if I was going to work with a hashtable it would be something like the following which may not work for you.

    Adapted from MSDN BinaryFormatter class example.

    Code:
    Sub Serialize()
        Dim ht As New Hashtable
    
        For x As Int32 = 1 To 1000
            ht.Add(x, "Item" & x.ToString)
        Next
    
        Dim fs As New FileStream("DataFile.dat", FileMode.Create)
        Dim formatter As New BinaryFormatter
        Try
            formatter.Serialize(fs, ht)
        Catch e As SerializationException
            Console.WriteLine("Failed to serialize. Reason: " & e.Message)
            Throw
        Finally
            fs.Close()
        End Try
    End Sub
    Sub Deserialize()
        Dim ht As Hashtable = Nothing
        Dim fs As New FileStream("DataFile.dat", FileMode.Open)
        Try
            Dim formatter As New BinaryFormatter
            ht = DirectCast(formatter.Deserialize(fs), Hashtable)
        Catch e As SerializationException
            Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
            Throw
        Finally
            fs.Close()
        End Try
    
        '
        ' Uncomment to see the data
        '
        'For Each de As DictionaryEntry In ht
        '    Console.WriteLine("{0}: {1}.", de.Key, de.Value)
        'Next
    
        TextBox1.Text = CStr((From T In ht.Cast(Of DictionaryEntry)() Select CInt(T.Key)).FirstOrDefault + 1)
    End Sub

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