Solved

Formatted Address

Posted on 2007-11-19
4
216 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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