rocky050371
asked on
Formatted Address
I have the following function that returns a block address, is there a better way of doing this, it seems a bit lengthy.
Public Shared Function ReturnFormattedAddress(ByV al address As StandardAddress) As String
Dim sFormattedAddress As String = ""
If Not address.Address1 = "" Then
sFormattedAddress &= address.Address1 & Environment.NewLine
End If
If Not address.Address2 = "" Then
sFormattedAddress &= address.Address2 & Environment.NewLine
End If
If Not address.Town = "" Then
sFormattedAddress &= address.Town & Environment.NewLine
End If
If Not address.County = "" Then
sFormattedAddress &= address.County & Environment.NewLine
End If
If Not address.Country = "" Then
sFormattedAddress &= address.Country & Environment.NewLine
End If
If Not address.Postcode = "" Then
sFormattedAddress &= address.Postcode & Environment.NewLine
End If
If Not sFormattedAddress = "" Then
sFormattedAddress = sFormattedAddress.Remove(s FormattedA ddress.Len gth - 1, 1)
End If
Return sFormattedAddress
End Function
Public Shared Function ReturnFormattedAddress(ByV
Dim sFormattedAddress As String = ""
If Not address.Address1 = "" Then
sFormattedAddress &= address.Address1 & Environment.NewLine
End If
If Not address.Address2 = "" Then
sFormattedAddress &= address.Address2 & Environment.NewLine
End If
If Not address.Town = "" Then
sFormattedAddress &= address.Town & Environment.NewLine
End If
If Not address.County = "" Then
sFormattedAddress &= address.County & Environment.NewLine
End If
If Not address.Country = "" Then
sFormattedAddress &= address.Country & Environment.NewLine
End If
If Not address.Postcode = "" Then
sFormattedAddress &= address.Postcode & Environment.NewLine
End If
If Not sFormattedAddress = "" Then
sFormattedAddress = sFormattedAddress.Remove(s
End If
Return sFormattedAddress
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can do like this (see code snippet below).
A StringBuilder is a convenient way of concatenating strings when you don't know exactly from the start how many strings there are.
The &= operator doesn't append anything to an existing string as one might think, instead it calls String.Concatentate to create a new string from the current string and the added string. This performs worse and worse the longer the string gets, as the entire string is copied every time. For every added &= operation, the execution time doubles, so it scales very badly. When using a StringBuilder the execution time is linear to the number of strings added, rather than exponential.
I've made a correction from how your original function works. Instead of removing half of the last line break (a line break is two characters in a Windows system), the function removes the entire line break. (You would use Environment.NewLine.Length if you need the length of a line break.)
A StringBuilder is a convenient way of concatenating strings when you don't know exactly from the start how many strings there are.
The &= operator doesn't append anything to an existing string as one might think, instead it calls String.Concatentate to create a new string from the current string and the added string. This performs worse and worse the longer the string gets, as the entire string is copied every time. For every added &= operation, the execution time doubles, so it scales very badly. When using a StringBuilder the execution time is linear to the number of strings added, rather than exponential.
I've made a correction from how your original function works. Instead of removing half of the last line break (a line break is two characters in a Windows system), the function removes the entire line break. (You would use Environment.NewLine.Length
Public Shared Function ReturnFormattedAddress(ByVal address As StandardAddress) As String
Dim builder As New StringBuilder()
For Each line As String In New String() {address.Address1, address.Address2, address.Town, address.County, address.Country, address.Postcode}
If line.Length > 0 Then
If builder.Length > 0 Then builder.AppendLine()
builder.Append(line)
End If
Next
Return builder.ToString()
End Function
(EE just threw away the code snippet I attached. Here it is again:)
Public Shared Function ReturnFormattedAddress(ByV al address As StandardAddress) As String
Dim builder As New StringBuilder()
For Each line As String In New String() {address.Address1, address.Address2, address.Town, address.County, address.Country, address.Postcode}
If line.Length > 0 Then
If builder.Length > 0 Then builder.AppendLine()
builder.Append(line)
End If
Next
Return builder.ToString()
End Function
Public Shared Function ReturnFormattedAddress(ByV
Dim builder As New StringBuilder()
For Each line As String In New String() {address.Address1, address.Address2, address.Town, address.County, address.Country, address.Postcode}
If line.Length > 0 Then
If builder.Length > 0 Then builder.AppendLine()
builder.Append(line)
End If
Next
Return builder.ToString()
End Function
ASKER