I am making a windows form in vb.net that needs to read form a text file and insert the text as objects when the form loads . The textboxes are as follow
-
TxtJobtitle.Text
-
TxtSalary.Text
-
TxtFullname.Text
-
TxtAdd1.Text
-
TxtAdd2.Text
-
TxtTown.Text
-
TxtCounty.Text
-
TxtPostcode.Text
-
TxtPhone.Text
-
TxtEmail.Text
-
i have this code to save it to the text file (not sure if its correct)
-
Private Sub CmdSaveall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSaveall.Click
-
Dim sw As New StreamWriter("Staff.txt")
-
counter = 0
-
While counter < collection.Length
-
sw.WriteLine(collection(counter).Jobtitle)
-
sw.WriteLine(collection(counter).Salary)
-
sw.WriteLine(collection(counter).Fullname)
-
sw.WriteLine(collection(counter).Add1)
-
sw.WriteLine(collection(counter).Add2)
-
sw.WriteLine(collection(counter).Town)
-
sw.WriteLine(collection(counter).County)
-
sw.WriteLine(collection(counter).Postcode)
-
sw.WriteLine(collection(counter).Phone)
-
sw.WriteLine(collection(counter).Email)
-
-
counter += 1
-
End While
-
sw.Close()
-
So what would be the best way to do this been trying to do it for 5 hours now and tryed a load of different ways but nothings worked
Seems ok to me...so long as the "collections" array contains an object with all of the properties that you are referring to....
eg:
-
Private Class CollectionItem
-
Public Property Jobtitle As String
-
Get
-
return TxtJobtitle.Text
-
End Get
-
End Property
-
Public Property SalaryAs String
-
Get
-
return TxtSalary.Text
-
End Get
-
End Property
-
.....
-
End Class
-
If this is the case you should be casting to that type
-
While counter < collection.Length
-
sw.WriteLine(CType(collection(counter),CollectionItem).Jobtitle)
-
sw.WriteLineCType(collection(counter),CollectionItem).Salary)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Fullname)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Add1)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Add2)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Town)
-
sw.WriteLine(CType(collection(counter),CollectionItem).County)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Postcode)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Phone)
-
sw.WriteLine(CType(collection(counter),CollectionItem).Email)
-
-
counter += 1
-
End While
-
In a better design you could make the CollectionItem Class have function like ToString, or PrintDetails or something that returns a string with all of this information formatted for printing ;)
That way, instead of calling each individual property, you could only make 1 function call.
Are you getting any errors when you run your code?
-Frinny