Page 1 of 2 12 LastLast
Results 1 to 40 of 61

Thread: [RESOLVED] How to write a generic contact List to a file that's inside a List(of contacs)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Resolved [RESOLVED] How to write a generic contact List to a file that's inside a List(of contacs)

    Hi everyone. I've been working on things with this type code for a little but, and I only just now wondered how to write out the generic list of contacts inside the List(of contacts). I hope that makes sense. Maybe it will when you see some code. I tried searching on the net, but it doesn't understand what I'm searching for.

    So, here's the declarations.

    Code:
    Public newContact As New Contact()
    Public contacts As New List(Of Contact)()
    I've had a number of people look at my post, but no one has said anything. Maybe it's because I'm not being clear. Here's more code that will hopefully let you see what I am trying to do.

    Code:
     With newContact
                .FirstName = firstName.Text
                .LastName = lastName.Text
                .PhoneNumber = PhoneNumber.Text
                .Birthday = Birthday.Value
                Using sw
                    sw = AppendText(theFilename)
                    sw.Write(.FirstName & "," & .LastName & "," & .PhoneNumber & "," & .Birthday)
                    sw.WriteLine()
                End Using
            End With
    So, newContact is a single class that contains the information associated with the class.
    Contacts is a List of newContact that contains all information for all instances of newContact.

    Disclaimer: I'm NEW at this stuff, so if the above explanation is not correct, I hope you know from the code what's going on cause.... I did my best guys...

    I want to make sure my approach to getting the information is sound, and looks half way good <smile>

    Since I can't get a search on the net to work, I will have to write my own code. Problem is, it would look like such a hack. It would work... just wouldn't want to show it to my friends. I'm pretty sure the logic will be centered around the Contacts count (with the "S" on the end of Contact)

    I'm not exactly looking for someone to give me some pretty code. But if you can let me know a better way to search to find the answers or a link to some site, that would be great.

    Thanks for any help. I do appreciate any help.

    If you need more info, just ask.

    edit: writing to a file is no problem... don't need help there <smile>
    Last edited by jumper77; Apr 16th, 2016 at 09:47 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    So, you're saying that you know how to write one Contact to a file but you want to know how to write all items in a List(Of Contact) to the file, correct? Just like walking is just taking one step multiple times, so writing a List(Of Contact) is just writing one Contact multiple times. You would write a For Each loop to enumerate the List and then, inside the loop, you would write the one current Contact to the file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Hi jmc, thanks from writing. I was thought that the "For each" loop would be the way to go. And I think that using "count" would be part of then loop as well.

    Give me some time to wake up (coffee is still making) and I'll start looking at the code. Once I have something that seems to work, I'll post it to see what you think.

    Thanks a bunch

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Quote Originally Posted by jumper77 View Post
    And I think that using "count" would be part of then loop as well.
    Nope. You just perform the desired action once For Each item in the list. The number of items is irrelevant. That only matters when using a For loop because that means getting items by index so you have to know what the upper bound of that index is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Ok, thanks for the information. Glad I don't have to use count. Makes it easier on me. I going to work on it some more and when I get something together I will post it and maybe you can tell if I'm doing it right or not.

    thanks a bunch

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Starting to wake up a little... couple of dumb questions (I was never good a for loops). I'm thinking that the loop should be a nested loop. Is that true? Also, "count" of the outer loop seems to be stuck and does not increase to the next value.

    The reason I say this is because I was watching in the debugger.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    I can't think why you need nested loops for this. You really need to show the code you are using.

    It should be as simple as:
    Code:
    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
    
        For Each someContact As Contact In contacts
            'Write the someContact.Info to file
        Next
    
    End Sub
    or similar.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Hi inferred Nice to see you. Guess I'm making this harder than it should be. I took out the nested part and the code below is what I lave left. Do you think this will work?
    Code:
            For Each person As Contact In contacts
                Write to file
            Next
    thanks to everyone for putting up with my thick (or dumb) head

  9. #9
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Should work. You're in the best position to know, though.....just try it

    If it doesn't work as expected, post back with the code you are using, and say what is happening that shouldn't be happening.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Here's the new code. And guess what? It works! yea!! I sure thought it would be harder than that. My problem is that even though I have used VB for a few years, I didn't know OOP. But I have some friends from here who are helping me get better at it. But because it seems to be more complicated to me, I don't expect solutions to be easy.

    Anyway.. here's the new code.

    Code:
            Dim writeFile As String = "tmpfile.txt"
            sw = AppendText(writeFile)
            For Each person As Contact In contacts
                Using sw
                    sw.Write("Hello")
                End Using
            Next
    Thank you Inferrd, and everyone who helped. You all took me to the answer without just handing me the code that was the solution. I like that. When you learn that way, you'll remember what you did more

  11. #11
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How to write a generic contact List to a file that's inside a List(of contacs)


  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    just found an Oops in the code. I kept getting one of my favorite errors for files. It's about not being able to do anything with the file because it was in use by another process. And as I'm sure you all know, the other process is us. I always managed to fix it, but never understood how or why.

    I guess code first:

    Code:
            Dim writeFile As String = "tmpfile.txt"
            'sw = AppendText(writeFile)
            For Each person As Contact In contacts
                Using sw
                    sw = AppendText(writeFile)
                    sw.WriteLine("Hello")
                End Using
            Next
    You'll see that I have commented out the first "AppendText" line. This was the offending code. With that code being there, I had to close the file before I could write to it. Didn't make "any" sense to me. So I commented out that line and everything worked perfectly (before I was getting that exception). So if anyone reads this who is as dumb as me, this would be a good lesson. It really taught me a good one

    thanks again everyone
    Last edited by jumper77; Apr 17th, 2016 at 08:17 AM.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Don't put the Using statement inside the loop. The whole point of a Using block is to dispose the object at the end of the block. Why would you want to dispose a StreamWriter every iteration of the loop? Think about it: open the file, loop through the list and write each item, close the file. The loop goes inside the Using block, not the other way around.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    I think you're right. When I first found "using" I thought it was really great. And I read where it took care of a lot of things like "flush" and "close", but as you said, doesn't make much sense in a loop. That would be the "start" of my issue about "is in use by another process". Let me rearrange my code, then I'll come back.

    thanks a lot!

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    That didn't take too long. here's the code

    Code:
            Dim writeFile As String = "tmpfile.txt"
            sw = AppendText(writeFile)
            For Each person As Contact In contacts
                sw.WriteLine(firstName.Text & "," & lastName.Text & "," & PhoneNumber.Text & "," & Birthday.Value)
            Next
            sw.Flush()
            sw.Close()
    I had to move the "AppendText" out of the loop because I started getting my favorite error again. Once I did that, everything's working great. I think I have it now. I think I have found all the gotcha's. I don't think I should put it in a try catch block, but not sure. What would you do?

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: How to write a generic contact List to a file that's inside a List(of contacs)

    Update to my write to file section. There was a point that I wasn't getting. I changed textbox.filename to person.filename.
    so here is that one line.

    Code:
     sw.WriteLine(person.FirstName & "," & person.LastName & "," & person.PhoneNumber & "," & person.Birthday)
    I think that is MUCH better than the ugly code I was doing before. Plus, it should work because I'm connecting directly to the object I need.
    Last edited by jumper77; Apr 17th, 2016 at 09:13 AM.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    If anyone is still listening to this thread... HELP!... I just now started paying attention to what was being written to the file inside the "for each" loop, and the behavior is odd. Let me explain:

    When you enter the first name (record) you want to save, it writes it to the file once.
    When you enter the second name you want to save, it writes it to the file twice.
    And when you enter the third name you want to save, it writes it to the file three times.

    And I'm sure that this would go on and on.

    If makes me feel like I should be removing a contact after I write it to the file. I think that would do it, but I don't know it that's the right thing to do.

    So if there is anyone out there listening (and you give a damn)<smile> let me know what you think.

    thanks a bunch

    edit: forgot code:
    Code:
     For Each person As Contact In contacts
                sw.WriteLine(person.FirstName & "," & person.LastName & "," & person.PhoneNumber & "," & person.Birthday)
            Next
    Another thing, person.count goes up every time you add a record. I'm guessing this has something to do with it???
    Last edited by jumper77; Apr 18th, 2016 at 05:50 PM.

  18. #18
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    How did you open the file? Odds are, you're opening the file for appending... so when you save, the first time, there's only one element. Then you add a second... since you append to the file, it writes the first item again, followed by the second... and so on... try just opening the file for write, not append, and see if you get the results you're looking for.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    OOOOOOOOOOOOOHhhhhhhhhhhhhhh that just might be it. I've been doing append now for so long. give me a minute and I'll give it a try

    thanks tg!

  20. #20

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Ok, I'm having a problem finding something quick. can you tell me what to use instead of append?

  21. #21
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Instead of AppendText, try CreateText

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    CreateText sounds like it would overwrite the file, but I'll try it. be back in a sec

  23. #23

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Nope... I think it's the count going up. and OF COURSE count is read only.

    *edit* CreateText works the same as append

  24. #24
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Don't know... the only other possibility is that what's in your list is in there multiple times...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  25. #25

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    I "think" I might have found something. If I put my own count in the loop and only let go through once that would work. But that's not a good solution because it could cost you in a big project. Still looking into what I can do.

  26. #26
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Whaaaaa?

    you should be doing the steps in this order:
    1) open the file
    2) for each contact in contactlist
    3) write out the contact info
    4) -next-
    5) close file

    that's it and nothing more... do you have a loop in a loop of some kind?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  27. #27

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    tg, the reason I said that is because that the number of times a record is written to the file is always Contacts.Count
    If count is 3, it writes the contact three times. And it just keeps going up as the count does.

    I don't really understand. Someone either gave me the "for each" loop or showed me some pseudo code.

    Here's the loop.
    Code:
            For Each person As Contact In contacts
                sw.WriteLine(person.FirstName & "," & person.LastName & "," & person.PhoneNumber & "," & person.Birthday)
            Next
    At the top of the file there's this.
    Code:
        Public newContact As New Contact()
        Public contacts As New List(Of Contact)()
    if you see anything wrong with all of this, PLEASE let me know. I can't figure it out.

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Quote Originally Posted by jumper77 View Post
    tg, the reason I said that is because that the number of times a record is written to the file is always Contacts.Count
    If count is 3, it writes the contact three times. And it just keeps going up as the count does.

    I don't really understand. Someone either gave me the "for each" loop or showed me some pseudo code.

    Here's the loop.
    Code:
            For Each person As Contact In contacts
                sw.WriteLine(person.FirstName & "," & person.LastName & "," & person.PhoneNumber & "," & person.Birthday)
            Next
    At the top of the file there's this.
    Code:
        Public newContact As New Contact()
        Public contacts As New List(Of Contact)()
    if you see anything wrong with all of this, PLEASE let me know. I can't figure it out.
    You're adding the same Contact object to the List multiple times instead of adding multiple distinct Contact objects. Get rid of that 'newContact' field. When you want to create a new Contact, declare a local variable there and then and create the object with the New keyword there and then. If you only use New once then you only have one object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  29. #29

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Sorry... had to go to the store:
    That kind of makes my brain twizzle. So dim a local object and it will be added to the list. That way it won't interfere with the list as a whole. I'm going to try that right now and see how it works. Thanks. I believe that you have found what I was doing wrong. Will let you know in a few minutes..

  30. #30
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Quote Originally Posted by jumper77 View Post
    So dim a local object and it will be added to the list.
    No. There's no local object. Your poor terminology is indicative of the issue. You declare a local VARIABLE and then you create multiple OBJECTS and assign them each to that variable in turn. The distinction between an object and a variable is critical. A variable is a place to put an object. A single variable can only refer to one object at a time but it can refer to different objects at different times. A single object can be referred to by multiple variables at the same time. It's just like real life. I am just one person but I can be referred to in many ways, e.g. myself, my sister's brother, my father's son, etc. Each of those references can be considered a variable while I am the object.

    Here's an example that demonstrates what you're currently doing:
    vb.net Code:
    1. Dim myThing As New Thing
    2. Dim things As New List(Of Thing)
    3.  
    4. For i = 1 To 3
    5.     myThing.Name = "Thing " & i
    6.     things.Add(myThing)
    7. Next
    8.  
    9. For Each theThing In things
    10.     Console.WriteLine(theThing.Name)
    11. Next
    In that case, there is only one Thing object. That same object gets added to the List three times. The last value that was assigned to that Thing object's Name property is "Thing 3" so that is what will be output three times at the end. This demonstrates what you should be doing:
    vb.net Code:
    1. Dim myThing As Thing
    2. Dim things As New List(Of Thing)
    3.  
    4. For i = 1 To 3
    5.     myThing = New Thing
    6.     myThing.Name = "Thing " & i
    7.     things.Add(myThing)
    8. Next
    9.  
    10. For Each theThing In things
    11.     Console.WriteLine(theThing.Name)
    12. Next
    In that case, a new Thing object is created on each iteration of the first loop so three distinct Thing objects are added to the to the List. Each object has its Name set to a different value so you will see three different outputs at the end. That code could also, and should also, be written like this:
    vb.net Code:
    1. Dim things As New List(Of Thing)
    2.  
    3. For i = 1 To 3
    4.     Dim myThing As New Thing
    5.  
    6.     myThing.Name = "Thing " & i
    7.     things.Add(myThing)
    8. Next
    9.  
    10. For Each theThing In things
    11.     Console.WriteLine(theThing.Name)
    12. Next
    By declaring the 'myThing' variable at the narrowest scope possible, you ensure that you can't make the mistake of creating too few objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  31. #31

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Well... it didn't work, but I may have not understood you well (actually that's quite possible). Here's the whole sub so you can see it. I just now put the top "New" in there

    Code:
        Private Sub addRecord()
            'msg.ShowMsg(Reflection.MethodBase.GetCurrentMethod().ToString)
    
            Dim newContact As New Contact()
            With newContact
                .FirstName = firstName.Text
                .LastName = lastName.Text
                .PhoneNumber = PhoneNumber.Text
                .Birthday = Birthday.Value
            End With
            contacts.Add(newContact)
    
            Dim sw As IO.StreamWriter
            sw = IO.File.AppendText("logfile.txt")
            Dim ex As Exception = Nothing
            sw.WriteLine(Reflection.MethodBase.GetCurrentMethod().ToString)
            sw.Flush() : sw.Close()
    
            Dim contactFile As String = "Contacts.txt"
            sw = IO.File.CreateText(contactFile)
    
            For Each person As Contact In contacts
                sw.WriteLine(person.FirstName & "," & person.LastName & "," & person.PhoneNumber & "," & person.Birthday)
            Next
            sw.Flush() : sw.Close()
            msg.ShowMsg(newContact.FirstName & " " & newContact.LastName & " Saved")
        End Sub
    *edit* code hasn't been cleaned up so it has a lot of junk in there that I'm not using. mostly the exception part
    Last edited by jumper77; Apr 18th, 2016 at 08:28 PM.

  32. #32

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Hi jmc. good to hear from you. Just want to let you know that I believe you know the answer and I'm working on understanding it so I can apply it to my code. I'll talk more in just a bit. I'm sorry but I missed your post. I do wish I had noticed it sooner.

  33. #33

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    jmc, back again. It's going to take me a bit to get this working because I have to learn the behavior of your code, and also learn how/where to apply it. Thank you so much. Your last post holds the key to solving this, I'm pretty sure. But won't know until tomorrow. Right now my blood pressure is too high

    And it's normally 120/80

  34. #34

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    YES!! The brain damage has subsided. I said I was going to leave it alone, but just couldn't. But the good news is.... It's now working. The answer came from 2 people helping where each other seemed to be filling me in on the parts the other person didn't mention. And it worked out great.

    So thank you jmc and thank you inferrd for all your help. I cleaned up the code so I could post it, so here it is...

    Code:
        Private Sub addRecord()
    
            Dim person As New Contact()
            Dim contacts As New List(Of Contact)()
    
            person.FirstName = firstName.Text
            person.LastName = lastName.Text
            person.PhoneNumber = PhoneNumber.Text
            person.Birthday = Birthday.Value
            contacts.Add(person)
    
            Dim contactFile As String = "Contacts.txt"
            sw = IO.File.AppendText(contactFile)
    
            For Each newPerson As Contact In contacts
                sw.WriteLine(newPerson.FirstName & "," & newPerson.LastName & "," & newPerson.PhoneNumber & "," & newPerson.Birthday)
            Next
            sw.Flush() : sw.Close()
            msg.ShowMsg(person.FirstName & " " & person.LastName & " Saved")
        End Sub
    Now this code only goes through the "for each" function only once for each record. I know I'm thick headed and it takes a while to get something through my head, but when it does, I have more and more understanding of what's going on.

    so thanks everyone. And I do mean EVERYONE

    Good night all

  35. #35

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Not awake yet (4:30 AM), but I just thought of something. Now that the code is working and it only goes through the loop one time, I don't really need the loop. I should just be able to have the one writeline and that's it.

    Going to try it and see what happens.

    And good morning everyone. Hope you had a great night

  36. #36

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Yep, at least I was right about something. There is no need for a loop at all, now that only one record at a time is written to the file. Plus, now I can put into a "using" block. I love those things.

    Now, something that needs to be said.... tg, I forgot to mention you for your part in helping me to understand. And everyone of you get a +1 for a rep

    *edit* Oh, I forgot one other person. Thanks to inferrd for help. +1 for you as well
    Last edited by jumper77; Apr 19th, 2016 at 04:56 AM.

  37. #37
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Quote Originally Posted by jumper77 View Post
    Oh, I forgot one other person. Thanks to inferrd for help. +1 for you as well
    Hey! You thanked Inferrd in post #34 and now you've thanked them again. That's not fair. Now you have to thank the rest of us a second time too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  38. #38

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Quote Originally Posted by jmcilhinney View Post
    Hey! You thanked Inferrd in post #34 and now you've thanked them again. That's not fair. Now you have to thank the rest of us a second time too.
    Oh no!! Oops. Thank you again jmc and tg. Don't want to be unfair. All you have helped so much and I can't thank you enough.

  39. #39
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Quote Originally Posted by jumper77 View Post
    Not awake yet (4:30 AM), but I just thought of something. Now that the code is working and it only goes through the loop one time, I don't really need the loop. I should just be able to have the one writeline and that's it.

    Going to try it and see what happens.

    And good morning everyone. Hope you had a great night
    As long as you only have one contact that works. What if you have two?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  40. #40

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of

    Hi db, I have tested it up to 4 contacts and it still works the same way.

    *edit* I should have also said this: on the window where you enter the information. the information is not saved until you clicked the "save" button. So you are only allowed to enter a contact one at a time.
    Last edited by jumper77; Apr 19th, 2016 at 06:31 AM.

Page 1 of 2 12 LastLast

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