Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatted Address

Posted on 2007-11-19
4
Medium Priority
?
238 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

704 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