473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading form a text file

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
3 1394
Frinavale
9,735 Expert Mod 8TB
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
DN2UK
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
9,735 Expert Mod 8TB
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

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

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: Joel Goldstick | last post by:
I wanted to write a simple page to let me choose a directory and then list the files in it. The end goal was to make an easy way to copy all the file names in a directory. I tested with Opera7,...
2
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
3
by: Luqman | last post by:
How can I retrieve text from html file and write to textbox. Best Regards, Luqman
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
3
by: lizii | last post by:
i have a file - which on each line has some data i need to fill into a box - now although reading in the data is simple enough and putting it in the correct box will be no problem, as i can just...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.