Solved

Formatted Address

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now