By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,035 Members | 623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,035 IT Pros & Developers. It's quick & easy.

Reading form a text file

P: 2
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
Expand|Select|Wrap|Line Numbers
  1. TxtJobtitle.Text
  2. TxtSalary.Text
  3. TxtFullname.Text
  4. TxtAdd1.Text
  5. TxtAdd2.Text
  6. TxtTown.Text
  7. TxtCounty.Text
  8. TxtPostcode.Text
  9. TxtPhone.Text
  10. TxtEmail.Text
  11.  
i have this code to save it to the text file (not sure if its correct)
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdSaveall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSaveall.Click
  2. Dim sw As New StreamWriter("Staff.txt")
  3. counter = 0
  4. While counter < collection.Length
  5. sw.WriteLine(collection(counter).Jobtitle)
  6. sw.WriteLine(collection(counter).Salary)
  7. sw.WriteLine(collection(counter).Fullname)
  8. sw.WriteLine(collection(counter).Add1)
  9. sw.WriteLine(collection(counter).Add2)
  10. sw.WriteLine(collection(counter).Town)
  11. sw.WriteLine(collection(counter).County)
  12. sw.WriteLine(collection(counter).Postcode)
  13. sw.WriteLine(collection(counter).Phone)
  14. sw.WriteLine(collection(counter).Email)
  15.  
  16. counter += 1
  17. End While
  18. sw.Close()
  19.  
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
Mar 11 '08 #1
Share this Question
Share on Google+
3 Replies

Frinavale
Expert Mod 5K+
P: 9,731
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
Expand|Select|Wrap|Line Numbers
  1. TxtJobtitle.Text
  2. TxtSalary.Text
  3. TxtFullname.Text
  4. TxtAdd1.Text
  5. TxtAdd2.Text
  6. TxtTown.Text
  7. TxtCounty.Text
  8. TxtPostcode.Text
  9. TxtPhone.Text
  10. TxtEmail.Text
  11.  
i have this code to save it to the text file (not sure if its correct)
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdSaveall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSaveall.Click
  2. Dim sw As New StreamWriter("Staff.txt")
  3. counter = 0
  4. While counter < collection.Length
  5. sw.WriteLine(collection(counter).Jobtitle)
  6. sw.WriteLine(collection(counter).Salary)
  7. sw.WriteLine(collection(counter).Fullname)
  8. sw.WriteLine(collection(counter).Add1)
  9. sw.WriteLine(collection(counter).Add2)
  10. sw.WriteLine(collection(counter).Town)
  11. sw.WriteLine(collection(counter).County)
  12. sw.WriteLine(collection(counter).Postcode)
  13. sw.WriteLine(collection(counter).Phone)
  14. sw.WriteLine(collection(counter).Email)
  15.  
  16. counter += 1
  17. End While
  18. sw.Close()
  19.  
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:
Expand|Select|Wrap|Line Numbers
  1. Private Class CollectionItem
  2.     Public Property Jobtitle As String
  3.          Get 
  4.             return TxtJobtitle.Text
  5.          End Get
  6.     End Property
  7.     Public Property SalaryAs String
  8.          Get 
  9.             return TxtSalary.Text
  10.          End Get
  11.     End Property
  12. .....
  13. End Class
  14.  
If this is the case you should be casting to that type
Expand|Select|Wrap|Line Numbers
  1. While counter < collection.Length
  2. sw.WriteLine(CType(collection(counter),CollectionItem).Jobtitle)
  3. sw.WriteLineCType(collection(counter),CollectionItem).Salary)
  4. sw.WriteLine(CType(collection(counter),CollectionItem).Fullname)
  5. sw.WriteLine(CType(collection(counter),CollectionItem).Add1)
  6. sw.WriteLine(CType(collection(counter),CollectionItem).Add2)
  7. sw.WriteLine(CType(collection(counter),CollectionItem).Town)
  8. sw.WriteLine(CType(collection(counter),CollectionItem).County)
  9. sw.WriteLine(CType(collection(counter),CollectionItem).Postcode)
  10. sw.WriteLine(CType(collection(counter),CollectionItem).Phone)
  11. sw.WriteLine(CType(collection(counter),CollectionItem).Email)
  12.  
  13. counter += 1
  14. End While
  15.  

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
Mar 11 '08 #2

P: 2
this is part of my class file not sure if its correct
Expand|Select|Wrap|Line Numbers
  1. Public Class Staff
  2.     Private m_Jobtitle As String
  3.     Private m_Salary As String
  4.     Private m_Fullname As String
  5.     Private m_Add1 As String
  6.     Private m_Add2 As String
  7.     Private m_Town As String
  8.     Private m_County As String
  9.     Private m_Postcode As String
  10.     Private m_Phone As String
  11.     Private m_Email As String
  12.  
  13.     Public Property Jobtitle() As String
  14.         Get
  15.             Return m_Jobtitle
  16.         End Get
  17.         Set(ByVal value As String)
  18.             m_Jobtitle = value
  19.         End Set
  20.     End Property
  21.  

how would i read them into the program then
Mar 11 '08 #3

Frinavale
Expert Mod 5K+
P: 9,731
this is part of my class file not sure if its correct
...
how would i read them into the program then
Have you researched the File Class?

What you're going to have to do is read the text file into your program and then parse it to fill your objects.

So if your text file looks like
Expand|Select|Wrap|Line Numbers
  1. Jobtitle 
  2. Salary 
  3. MyFirstName MyLastName
  4. 9222 Hypothetical Dive StateOrProvince
  5. 2 Hypothetical2 Dive StateOrProvince
  6. CountryName
  7. TownName
  8. County 
  9. Postcode 
  10. Phone
  11. Email
  12.  
You would parse your file by looking for new lines in the text file.
When you come across the first new line, you know that the first string you've gathered is the "job title"...the second "salary"...and so on.


-Frinny
Mar 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.