Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Reverse the process.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [RESOLVED] [2005] Reverse the process.

    Hello below is a sub that writes a bunch of information to a file.
    Basicly it is in a way a 'save' method for my application. I now want to create a 'load' so to do this i will have to reverse this process, how may i do this? Note that since i am recording info through a for each loop the loops and the means of how information is retrieved must be in the same order.

    Here is my code:

    vb Code:
    1. 'Here:
    2. Private Sub AddCustomer()
    3.         Dim Customer_Info As New List(Of String)
    4.         For Each ctrl As Control In Me.CustomerEditArea.Controls
    5.             If Not (TypeOf (ctrl) Is Label) And Not (TypeOf (ctrl) Is CheckBox) And Not (TypeOf (ctrl) Is DateTimePicker) Then
    6.                 Customer_Info.Add(ctrl.Text.Trim)
    7.             End If
    8.         Next
    9.         Customer_Info.Add(DateSelecter.Value.ToString)
    10.         For Each chk As CheckBox In Me.CustomerEditArea.Controls
    11.             If TypeOf (chk) Is CheckBox Then
    12.                 Customer_Info.Add(chk.CheckState.ToString)
    13.             End If
    14.         Next
    15.         Customer_Info.Add(lblsubtotal.Text)
    16.         Customer_Info.Add(lbltotal.Text)
    17.         IO.File.WriteAllLines(ProfileDirectory & txtfirstname.Text.Trim & txtlastname.Text.Trim & ".ERcr", Customer_Info.ToArray)
    18.     End Sub

  2. #2
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2005] Reverse the process.

    First of all, I'd suggest that you try XML serialization, it should solve many of your worries with one stroke.

    If you decide to stick with this method, try this:
    Code:
    Dim filepath As String '= file path here
    Dim lines() As String = IO.File.ReadAllLines(filepath)
    Dim i As Integer
    For Each ctrl As Control In Me.CustomerEditArea.Controls
         If Not (TypeOf (ctrl) Is Label) And Not (TypeOf (ctrl) Is CheckBox) And Not (TypeOf (ctrl) Is DateTimePicker) Then
              ctrl.Text = lines(i)
              i += 1
         End If
    Next
    For Each chk As CheckBox In Me.CustomerEditArea.Controls
         If TypeOf (chk) Is CheckBox Then
              chk.CheckState = CType(lines(i), CheckState)    'This line will work for sure if you change the chk.CheckState.ToString to a CStr(chk.CheckState), otherwise I'm not sure
              'If it doesn't and you don't want to change your code, you'll need to insert a Select Case block
              i += 1
         End If
    Next
    lblsubtotal.Text = lines(i)
    lbltotal.Text = lines(i+1)
    If the code above doesn't work, post the exception and I'll do my best to fix it.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Reverse the process.

    In the code you supplied you are missing line 9 of my code:

    Customer_Info.Add(DateSelecter.Value.ToString)
    How would i place that in? (between the first and second for each loop)

  4. #4
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2005] Reverse the process.

    Hmmm... Is DateSelecter a DateTimePicker? I'm not exactly sure which date format will the ToString method return, that should be checked, but you'd use Date.Parse and set it to the Value property I think... Unfortunately, I haven't used a DateTimePicker and I have no idea which format will be used in ToString, as for some reason I cannot open the MSDN webpage... :S I'll try again in the morning, I need to get some sleep now...

    Btw, this is exactly why I suggested using XML serialization... When serializing a class or structure, you'd keep a String as a String, a Date as a Date, an Enum as that same Enum and a List(Of T) as a List(Of T). You'd only have to worry about saving the data in the class and retrieving and using it afterwards...
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Reverse the process.

    Yes it is a DateTimePicker...

    Then just wondering, how would XML serialization work?

  6. #6
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2005] Reverse the process.

    It would be something like this:
    Code:
    Public Structure Data
        Public texts As List(Of String)
        Public checkboxes As List(Of CheckState)
        Public dt As Date
        Public total As String
        Public subtotal As String
    End Structure
    
    'You'd populate an instance of this structure and then:
    Public Sub Serialize(ByVal data As Data)
        Dim serializer As New Xml.Serialization.XmlSerializer(GetType(data)))
        Dim fs As New IO.FileStream("test.xml", IO.FileMode.Create)
        serializer.Serialize(fs, items)
        fs.Close()
    End Sub
    
    'When you want to recover the data:
    Public Function Deserialize() As Data
        Dim dat As Data
        Dim serializer As New Xml.Serialization.XmlSerializer(GetType(data)))
        Dim sReader As New IO.StreamReader("test.xml")
        dat = DirectCast(serializer.Deserialize(sReader), Data)
        sReader.Close()
        Return dat
    End Function
    You can also serialize a class instance, not only a structure. The only prerequisite is that the types you use must have parameterless constructors.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  7. #7
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2005] Reverse the process.

    So how do you like it? Have you decided on what to use?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Reverse the process.

    I think i might stick to post #2. Since it seems easier and works better with my code.

  9. #9
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [RESOLVED] [2005] Reverse the process.

    As you wish. Then use the following to store and retrieve the date:
    Code:
    'This will give you the date as a String
    Dim dat As New Date = Date.Now 
    dat.ToString(CultureInfo.InvariantCulture)
    
    'And this will convert it back to a DateTime
    Dim dateString As String = "5/1/2008 8:30:52 AM"
    Dim dat As Date = Date.Parse(dateString, CultureInfo.InvariantCulture)
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [RESOLVED] [2005] Reverse the process.

    Thanks

  11. #11
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [RESOLVED] [2005] Reverse the process.

    Anytime Glad to be able to help
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [RESOLVED] [2005] Reverse the process.

    if i were to write the dateselector.value on the first line then for loading couldnt i do this?

    Code:
    DateSelecter.Value = (Date.Parse(lines(0)))
    Since for this one program i am using VS 2005 and Culture.. doesnt exist unless i have to import something..

  13. #13
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [RESOLVED] [2005] Reverse the process.

    It's System.Globalization.Culture and it's an assembly from .Net 2.0 so it must exist in VS 2005.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [RESOLVED] [2005] Reverse the process.

    ok ill check it, but just wondering then what i wrote will that work to or it will result in error?

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