-
[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>
-
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.
-
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
-
Re: How to write a generic contact List to a file that's inside a List(of contacs)
Quote:
Originally Posted by
jumper77
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.
-
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
-
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.
-
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.
-
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 :)
-
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.
-
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 :)
-
Re: How to write a generic contact List to a file that's inside a List(of contacs)
-
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
-
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.
-
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!
-
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?
-
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.
-
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???
-
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
-
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!
-
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?
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Instead of AppendText, try CreateText
-tg
-
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
-
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
-
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
-
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.
-
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
-
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.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
jumper77
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.
-
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..
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
jumper77
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:
Dim myThing As New Thing
Dim things As New List(Of Thing)
For i = 1 To 3
myThing.Name = "Thing " & i
things.Add(myThing)
Next
For Each theThing In things
Console.WriteLine(theThing.Name)
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:
Dim myThing As Thing
Dim things As New List(Of Thing)
For i = 1 To 3
myThing = New Thing
myThing.Name = "Thing " & i
things.Add(myThing)
Next
For Each theThing In things
Console.WriteLine(theThing.Name)
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:
Dim things As New List(Of Thing)
For i = 1 To 3
Dim myThing As New Thing
myThing.Name = "Thing " & i
things.Add(myThing)
Next
For Each theThing In things
Console.WriteLine(theThing.Name)
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.
-
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
-
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.
-
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
-
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
-
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
-
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 :)
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
jumper77
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. ;)
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
jmcilhinney
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. :)
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
jumper77
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?
-
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.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
dbasnett
As long as you only have one contact that works. What if you have two?
It doesn't matter since he's always creating the list and contact instance on every save. The list and the loop isn't needed with the way it is setup currently.
Now, later when he gets around to loading and what not... then he'll need the list (at a different level though) and the loop.
-tg
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
This is way off topic, but I wanted to tell you all something. My lady has been looking for another house to buy for 3 and a half years. And we finally found one. It's a double wide trailer that looks brand new inside. I mean it is perfect. It's about 7 years old, but it looks like it came off the lot a few days ago. A lot of people look down on trailers, but I don't. This one is way better than any house we've looked at.
Here's some more things that are great about it. It's in a "tiny" town. Population a little over 1,400 (that's so cool). But the closest big town is only about 10 minutes away. While we were looking at the house, not one car went down the street. And it's so quiet that my lady was almost whispering when we were talking.
And here's the kicker. One of the main reasons we want to get out of town is this... I live in a town that rates #2 in the nation for the most violent crimes per capita. We also live about a block from the biggest hospital. So, in addition to hearing gun shots every night while sitting on our deck, we also get a "lot" of helicopters going in and out. And of course, ambulances going all day long with their sirens blaring.
It's still not a done deal yet, but we'll know in a day or two. Sorry about this "outburst", but you guys are the closest thing I have to a family, so I wanted to share it with you.
now we return you to our regular scheduled program :)
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
And it's so quiet that my lady was almost whispering when we were talking.
BUY IT!! Any property that has that effect on the Missus will pay dividends later :)
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
aw thank you Dex. Unlike me, she stays up real late then sleeps in until about 11:00am. But when she's awake good (about 1:00pm) She will give them a call. I'm amazed that it's still on the market. But these days people want to be close to everything that little town doesn't have. I understand that because I used to think that too. But that old saying that "older makes you wiser" isn't really true <grin>. I just look at things a little differently now. When I was younger, I would have considered this town to be "in the sticks". Now I love it for the same reason.
Don't mean to go on and on. But it's a big thing for us and looking for 3 1/2 years is a long time.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Everytine I've moved, it's been to a progrssively smaller & less populated place... I'm on the edge of town now... and I'm almost ready to move out furhter into the coutnry.
I think after the kids are gone, we'll buy a piece of land somewhere nice and get one of those "tiny homes" where the sqf is less than 1000 ... if its just the two if us, dog and cat, it should be fine. Has to be close to the lake though so I can take the kayak out when I want.
-tg
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
That's great tg, seems like we think alike. It's been the same with me. Moving to a less and less populated area. And I love those "tiny homes". I've looked at them for a long time. If you've been looking at them, you probably know about this.. I also love "Cob" houses! They are incredible. We have one here. It's way out of town and way off the grid. Uses solar panels for electricity. At one time I loved the thought about being off the grid until I realized that meant no Internet (oh no).
Stick by that dream of getting out of town and moving to the lake. I love to fish, so that would be a great choice for me too.
Need to get some coffee, so I'll close this one now.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
To be honest, I am surprised that nobody has suggested using serialization. You can use simple XML serialization to convert your class into an XML file or convert your List(Of Contacts) into a single XML file with multiple nodes. Take a look at this example:
Code:
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Xml.Serialization
Public Module Module1
Public Sub Main()
Dim dday9 As New Contact With {.FirstName = "dday", .LastName = "9", .PhoneNumber = "555-555-1234", .DateOfBirth = New Date(1991, 08, 12)}
Dim dday10 As New Contact With {.FirstName = "dday", .LastName = "10", .PhoneNumber = "985-555-1234", .DateOfBirth = Date.Now}
Dim collection As New List(Of Contact) From {dday9, dday10}
For Each value As Contact In collection
Contact.Serialize(value, String.Format("{0}{1}.xml", value.FirstName, value.LastName))
Next
End Sub
End Module
Public Class Contact
Private _firstName As String
Public Property FirstName As String
Get
Return _firstName
End Get
Set(ByVal value As String)
If _firstName <> value Then
_firstName = value
Me.OnFirstNameChanged()
End If
End Set
End Property
Private _lastName As String
Public Property LastName As String
Get
Return _lastName
End Get
Set(ByVal value As String)
If _lastName <> value Then
_lastName = value
Me.OnLastNameChanged()
End If
End Set
End Property
Private _phoneNumber As String
Public Property PhoneNumber As String
Get
Return _phoneNumber
End Get
Set(ByVal value As String)
If _phoneNumber <> value Then
_phoneNumber = value
Me.OnPhoneNumberChanged()
End If
End Set
End Property
Private _dateOfBirth As Date
Public Property DateOfBirth As Date
Get
Return _dateOfBirth
End Get
Set(ByVal value As Date)
If _dateOfBirth <> value Then
_dateOfBirth = value
Me.OnDateOfBirthChanged()
End If
End Set
End Property
Protected Overridable Sub OnFirstNameChanged()
RaiseEvent FirstNameChanged(Me, EventArgs.Empty)
End Sub
Protected Overridable Sub OnPhoneNumberChanged()
RaiseEvent PhoneNumberChanged(Me, EventArgs.Empty)
End Sub
Protected Overridable Sub OnLastNameChanged()
RaiseEvent LastNameChanged(Me, EventArgs.Empty)
End Sub
Protected Overridable Sub OnDateOfBirthChanged()
RaiseEvent DateOfBirthChanged(Me, EventArgs.Empty)
End Sub
Public Event FirstNameChanged(ByVal sender As Object, ByVal e As EventArgs)
Public Event LastNameChanged(ByVal sender As Object, ByVal e As EventArgs)
Public Event PhoneNumberChanged(ByVal sender As Object, ByVal e As EventArgs)
Public Event DateOfBirthChanged(ByVal sender As Object, ByVal e As EventArgs)
Public Shared Function Deserialize(ByVal path As String) As Contact
Dim sender As Contact
Dim serializer As New XmlSerializer(GetType(Contact))
Using reader As IO.StreamReader = New IO.StreamReader(path)
sender = DirectCast(serializer.Deserialize(reader), Contact)
End Using
Return sender
End Function
Public Shared Sub Serialize(ByVal sender As Contact, ByVal path As String)
Dim serializer As New XmlSerializer(GetType(Contact))
Dim xml As String = String.Empty
Using w As IO.StringWriter = New IO.StringWriter()
serializer.Serialize(w, sender)
End Using
End Sub
End Class
Fiddle: https://dotnetfiddle.net/FevTtr
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
To be honest, I am surprised that nobody has suggested using serialization. You can use simple XML serialization to convert your class into an XML file or convert your List(Of Contacts) into a single XML file with multiple nodes.
Wow dday. This stuff is awesome! It would be great if I could learn this. Back when XML was coming in, I didn't like the format and tried to ignore the movement. Not the greatest idea looking back.
If I could wrap my head around this it would be an enormous breakthrough for me. For me, it would be a study that would take quite a while to understand. And I would probably work on it full time too.
Wow, what is ".Net Fiddle"? A place to store your code?
Also, I'm going to send you a PM in just a second.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Yeah .NET Fiddle is just a playground that allows you to execute console application code. I use it if I need a quick and dirty test or to provide people with online examples(like forums).
The code is relatively simple. In the Serialize method, I have 2 arguments: 1 to reference the Contact to serialize(aka - save) and 1 to reference the location on the computer to serialize the Contact to. It then uses a StreamWriter to write the XML generated from the XmlSerializer's Serialize function to the location specified in the parameter.
In the Deserialize method, I only have 1 argument: 1 to reference the Contact to deserialize the information to(aka - load). It then uses a StreamReader to read the pre-generated XML to the Contact and finally returns the Contact with the information from the file.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
techgnome
It doesn't matter since he's always creating the list and contact instance on every save. The list and the loop isn't needed with the way it is setup currently.
Now, later when he gets around to loading and what not... then he'll need the list (at a different level though) and the loop.
-tg
This is another fluid situation. IF you have a list of contacts then a loop is probably needed. If you change the problem to be a single instance of contact, and not a list, then of course there would be no point in having a loop.
It is hard for me to keep up with whatever this is supposed to be about.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Yea dday, I really like that code, I'm going to download it and keep it on my desktop so I can look at it whenever....
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Hey dday, I want to ask you something about your code. This is the line:
Code:
Dim dday9 As New Contact With {.FirstName = "dday", .LastName = "9", .PhoneNumber = "555-555-1234", .DateOfBirth = New Date(1991, 08, 12)}
I've seen code similar to this before, but I've never used it myself. Here's what I'm thinking... At the time a declaring a new instance of Contact, you use the with statement to use the shorter version of the first names and second names.
Also, an odd thing for me to see in vb is the curly braces . If I were using C# then I wouldn't think about it. What is the significance of curly braces in Vb.net? I would like to know.
Well, off to play with code :)
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
What the line does is shorten this...:
Code:
Dim dday9 As New Contact
With dday9
.FirstName = "dday"
.LastName = "9"
.PhoneNumber = "555-555-1234"
.DateOfBirth = New Date(1991, 08, 12)
End With
...down to 1 line by initializing all of the properties using the inline With statement.
The curly brackets in Visual Basic .NET are typically used to initialize an array such as in this code:
Code:
Dim foo() As Integer = {1, 2, 3, 4 ,5}
But the curly brackets do have other uses such as the inline With statement.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
That's how VB offsets that code so it knows what to do with it. In this case it means "Create an object of this type WITH these properties pre-initialized."
It's a shorter version of this:
Code:
Dim dday9 As New Contact
With Contact
.FirstName = "dday"
.LastName = "9"
.PhoneNumber = "555-555-1234"
.DateOfBirth = New Date(1991, 08, 12)
End With
Personally I don't like the with keyword, except in a declaration line... but that's mostly just me.
-tg
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
thanks guys. And tg, it's funny but before this was posted, I wanted to search for something that could replace "With". Some other type of code. There are a couple of reasons I don't like it. One is that hovering your mouse over the variables do not show you the values. And the other thing.... well, it's just ugly :)
Is there something else that does the same thing but doesn't give you the creeps or bore you?
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
I use either this:
Code:
Dim dday9 As New Contact With {.FirstName = "dday", .LastName = "9", .PhoneNumber = "555-555-1234", .DateOfBirth = New Date(1991, 08, 12)}
or this:
Code:
Dim dday9 As New Contact
Contact.FirstName = "dday"
Contact.LastName = "9"
Contact.PhoneNumber = "555-555-1234"
Contact.DateOfBirth = New Date(1991, 08, 12)
I don't use the With any other way.
-tg
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
I have to admit that I'm a stubborn old guy. I would never remember dday's example and I don't like the normal code. So eventually I will come up with something. When I do, I'll show it to you. Heck, I might make it a whole function <grin>.
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Well, It didn't take me long to come up with my first one. And I KNOW that there are people who would beat me up over doing it this way but it doesn't bother me. My thoughts are, as long as the code is readable, there is no reason why you can't have more than one piece of code on one line...
like this:
Code:
With person.FirstName = firstName.Text : person.LastName = lastName.Text
person.PhoneNumber = PhoneNumber.Text : person.Birthday = Birthday.Value : End With
To me, it's way better looking than the standard "with" code.
And one last thing... this one is backwards from the last one.
Code:
With newContact
firstName.Text = .FirstName : lastName.Text = .LastName
PhoneNumber.Text = .PhoneNumber : Birthday.Value = .Birthday : End With
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Quote:
Originally Posted by
jumper77
Code:
With person.FirstName = firstName.Text : person.LastName = lastName.Text
person.PhoneNumber = PhoneNumber.Text : person.Birthday = Birthday.Value : End With
In that case, then why bother with the with? it serves no purpose. Since we're throwing opinions out there... I don't like multiple lines on a single line... it gets hard to read... even that one line was tough... if I found that in my shop, I'd be checking the file out and "fixing" it.
-tg
-
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
Shoot, I knew someone was going to beat me up (just kidding). You are right of course. I have never done anything like this anywhere I've worked, and probably wouldn't even use it in my own projects actually. The only defense I have is, I don't work for real anymore and I get to play around a lot. So I have two sets of standards.
And darn, I just remembered something. We don't want to expose people who may be new to the language to bad habits. And folks. This IS a bad habit. Don't try this at home OR AT WORK.