How I parsed a really long email to collect ~20 values

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
VB.NET:
    Public Function GetDispatchInformation(data As String) As Dispatch
        Dim NewDispatch As New Dispatch
        Dim sb As New StringBuilder
        Dim wordDelimiters() As Char = New Char() {vbCrLf}
        Dim wordDelimiters1() As Char = New Char() {":"c}
        Dim counter As Integer = 1
        For Each word As String In data.Split(wordDelimiters, StringSplitOptions.None)
            If word.Length < 3 Then
                'nothing
            Else
                If word.Contains("You have been assigned dispatch number") Then
                    'parse dispatch
                ElseIf word.Contains("Service Administrator:") Then
                    NewDispatch.ServiceAdministrator = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Purchased From:") Then
                    NewDispatch.Client = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Model:") Then
                    NewDispatch.Model = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Name:") Then
                    NewDispatch.Name = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Address") Then
                    'parse address
                ElseIf word.Contains("Home Phone:") Then
                    NewDispatch.ConsumerHomePhone = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Consumer Email:") Then
                    NewDispatch.ConsumerEmail = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Problem Description:") Then
                    NewDispatch.ProblemDescription = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Payment Type:") Then
                    NewDispatch.PaymentType = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("CRM Number:") Then
                    NewDispatch.CRMNumber = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Authorization Number:") Then
                    NewDispatch.AuthorizationNumber = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Authorization Amount:") Then
                    NewDispatch.AuthorizationAmount = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Date of Purchase:") Then
                    NewDispatch.DateOfPurchase = DateTime.Parse(word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf))
                ElseIf word.Contains("Brand:") Then
                    NewDispatch.Brand = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Service Site:") Then
                    NewDispatch.ServiceSite = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Mailing Label Method:") Then
                    NewDispatch.MailingLabelMethod = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement1:") Then
                    NewDispatch.Entitlement1 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement2:") Then
                    NewDispatch.Entitlement2 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement3:") Then
                    NewDispatch.Entitlement3 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement4:") Then
                    NewDispatch.Entitlement4 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement5:") Then
                    NewDispatch.Entitlement5 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement6:") Then
                    NewDispatch.Entitlement6 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                ElseIf word.Contains("Entitlement7:") Then
                    NewDispatch.Entitlement7 = word.Split(wordDelimiters1, StringSplitOptions.None)(1).Trim.Trim(vbCrLf)
                End If
                counter = counter + 1
            End If


        Next
        Return NewDispatch
    End Function

The emails look like this:
VB.NET:
You have been assigned dispatch number # which has been automatically accepted.
To review and view entitlements this dispatch, log on at #, open the Dispatch Inbox from
the Quick Links shown on the Main Menu, select 'Accepted' from the dropdown
and click on the Search button.  This will bring up the list of accepted Dispatches.
 
Service Administrator: NEW WarLoc
Purchased From: WAL-MART.COM
 
Model: E E1-571-6607
Serial Number: NXM09AA03032918BBD3400
 
 
Name: #
Address: #
Home Phone: #
Consumer Email: #
Problem Description: SCREEN IS CRACKED
Special Instructions: Cracked Screen
Payment Type: Service Contract
CRM Number: #
Authorization Number: #
Authorization Amount: 504.00
Date of Purchase: 08-NOV-2013
Brand: ACER001
Service Site: Depot
Mailing Label Method: MailContract Covers:
Entitlement1: EXCLUSION - FAILURES NOT COVERED UNDER PLAN - COMMERCIAL USE OF PRODUCT  PRODUCTS USED IN A BUSINESS ENVIRONMENT OR PRODUCTS SPECIFICALLY DESIGNED FOR NON-RESIDENTIAL USE.
Entitlement2: COVERED FAILURE - DAMAGE CAUSED BY POWER SURGE COVERED FROM DATE OF PURCHASE  PRIMARY COVERAGE BEGINS DATE OF PURCHASE
Entitlement3: COVERED FAILURE - ACCIDENTAL DAMAGE FROM SPILLS AND DROPS IS COVERED FROM DOP  LAPTOPS ONLY COVER ACCIDENTAL DAMAGE, SUCH AS SPILLS, DROPS FROM DOP
Entitlement4: DOP ENDORSEMENT  ADH-LAPTOPS
Entitlement5: PARTS UNDER  NEW
Entitlement6: LABOR UNDER  NEW
Entitlement7: EXCEPTIONAL PARTS UNDER  NA
Entitlement8: EXCEPTIONAL LABOR UNDER  NA
Out for Repair Number: NULL
 
Follow this link to view the dispatch: #

Is there a better way to do this?

Open for discussion/comments/advice.

Thanks,

Jonathan Coleman
 
You are both mixing different concepts.
Extension methods are instance methods that extends the type definition. They are defined in modules (where all members are Shared), and where first parameter represents the instance of the type that it extends. The method can work with the passed instance object. They are called like this: Dim returnValueIfFunction = instance.Extensionmethod(otherParameters)
You can't call it as extension of the Type, you have to use an instance.

This is different from a regular Shared factory method defined in type, where you don't pass an instance, but just qualify the call by type like this: Dim instance = TheType.SharedMethod(otherParameters)
or in the case of using a Module you can call the method directly by type promotion: Dim instance = SharedMethod(otherParameters) 'or = TheModule.SharedMethod(...)
Herman said:
Dim d = Teletrack.TTDispatch.FromText(Mail.Body)
This won't work because TTDispatch is a type and not an instance.
jwcoleman87 said:
Dim NewDispatch As New Teletrack.TTDispatch
Dim d = Teletrack.TTDispatchExtensions.FromText(NewDispatch, Mail.Body)
As Herman noted you could have called this as extension like this:
VB.NET:
        Dim newDispatch As New Teletrack.TTDispatch
        Dim d = newDispatch.FromText(Mail.Body)
The way you have coded this works, but it also means 'NewDispatch' is the same instance as 'd', which is kind of confusing and seems redundant. This is again the "same" code:
VB.NET:
        Dim newDispatch As New Teletrack.TTDispatch
        newDispatch = newDispatch.FromText(Mail.Body)

        'or disregarding return value:
        newDispatch.FromText(Mail.Body)
The extension method doesn't have to return a value, it could modify the passed instance:
<Extension()> Public Sub FillFromText(extInstance As TTDispatch, text As String)
        'do something with extInstance
        'no return value
End Sub

calling:
        Dim newDispatch As New Teletrack.TTDispatch
        newDispatch.FillFromText(Mail.Body)

As a regular factory method you could declare like this in Module (no <extension()>)
Public Function TTDispatchFromText(text As String) As TTDispatch
        Dim extInstance As New TTDispatch
        ' etc
        Return extInstance
End Function

and use it like this:
        Dim newDispatch = TTDispatchFromText(Mail.Body) ' or = Teletrack.TTDispatchExtensions.TTDispatchFromText(...)

To be able to call it like TTDispatch.FromText you have to add the method either to the original type definition (not possible here as I understand since it's generated) or in a Partial class (like you attempted earlier), and the method must be declared Shared.
 

Latest posts

Back
Top