Solved

Formatted Address

Posted on 2007-11-19
4
221 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
  • 2
4 Comments
 
LVL 85

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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