|
-
Apr 19th, 2016, 07:12 AM
#41
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
 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
-
Apr 19th, 2016, 08:28 AM
#42
Thread Starter
PowerPoster
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
Last edited by jumper77; Apr 19th, 2016 at 08:32 AM.
-
Apr 19th, 2016, 09:01 AM
#43
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
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
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Apr 19th, 2016, 09:14 AM
#44
Thread Starter
PowerPoster
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.
-
Apr 19th, 2016, 09:25 AM
#45
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
-
Apr 19th, 2016, 09:38 AM
#46
Thread Starter
PowerPoster
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.
-
Apr 19th, 2016, 10:31 AM
#47
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
-
Apr 19th, 2016, 10:42 AM
#48
Thread Starter
PowerPoster
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.
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.
-
Apr 19th, 2016, 10:48 AM
#49
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.
-
Apr 19th, 2016, 11:11 AM
#50
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
 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.
-
Apr 19th, 2016, 11:57 AM
#51
Thread Starter
PowerPoster
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....
-
Apr 19th, 2016, 02:02 PM
#52
Thread Starter
PowerPoster
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
-
Apr 19th, 2016, 02:16 PM
#53
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.
-
Apr 19th, 2016, 02:16 PM
#54
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
-
Apr 19th, 2016, 02:26 PM
#55
Thread Starter
PowerPoster
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?
-
Apr 19th, 2016, 02:36 PM
#56
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
-
Apr 19th, 2016, 02:40 PM
#57
Thread Starter
PowerPoster
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>.
-
Apr 19th, 2016, 02:47 PM
#58
Thread Starter
PowerPoster
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
Last edited by jumper77; Apr 19th, 2016 at 03:29 PM.
-
Apr 19th, 2016, 03:56 PM
#59
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
 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
-
Apr 19th, 2016, 04:03 PM
#60
Thread Starter
PowerPoster
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.
-
Apr 19th, 2016, 04:15 PM
#61
Thread Starter
PowerPoster
Re: [RESOLVED] How to write a generic contact List to a file that's inside a List(of
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.
Of course you are right. Sometimes I think I just get too out there on things. I don't always take into consideration that we are supposed to be examples for others when it comes to coding. I've been nailed a couple of times for posting code like this, and I understand why. And I really need to get that in my head.
So I apologize to you and all the good people here. And I'll do my best to keep myself under some kind of control.
Thanks for pointing this out tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|