Solved

Formatted Address

Posted on 2007-11-19
4
230 Views
Last Modified: 2012-05-05
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(ByVal 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(sFormattedAddress.Length - 1, 1)
        End If
        Return sFormattedAddress

    End Function
0
Comment
Question by:rocky050371
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 20314028
I don't see anything wrong with what you are doing...

The values being checked are not "sequential" like "A1", "A2", "A3", etc... so you have to hard code them in there somehow.

I would consider making a ToString() implementation for "StandardAddress" though instead of using a "Shared Function ReturnFormattedAddress".
0
 

Author Comment

by:rocky050371
ID: 20314120
Thank you
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20314295
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.)
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

Open in new window

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20314301
(EE just threw away the code snippet I attached. Here it is again:)

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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question