[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:
'Here:
Private Sub AddCustomer()
Dim Customer_Info As New List(Of String)
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
Customer_Info.Add(ctrl.Text.Trim)
End If
Next
Customer_Info.Add(DateSelecter.Value.ToString)
For Each chk As CheckBox In Me.CustomerEditArea.Controls
If TypeOf (chk) Is CheckBox Then
Customer_Info.Add(chk.CheckState.ToString)
End If
Next
Customer_Info.Add(lblsubtotal.Text)
Customer_Info.Add(lbltotal.Text)
IO.File.WriteAllLines(ProfileDirectory & txtfirstname.Text.Trim & txtlastname.Text.Trim & ".ERcr", Customer_Info.ToArray)
End Sub
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.
Re: [2005] Reverse the process.
In the code you supplied you are missing line 9 of my code:
Quote:
Customer_Info.Add(DateSelecter.Value.ToString)
How would i place that in? (between the first and second for each loop)
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...
Re: [2005] Reverse the process.
Yes it is a DateTimePicker...
Then just wondering, how would XML serialization work?
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.
Re: [2005] Reverse the process.
So how do you like it? Have you decided on what to use?
Re: [2005] Reverse the process.
I think i might stick to post #2. Since it seems easier and works better with my code.
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)
Re: [RESOLVED] [2005] Reverse the process.
Re: [RESOLVED] [2005] Reverse the process.
Anytime :) Glad to be able to help :D
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..
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.
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?