Solved

String manipulation issue

Posted on 2011-03-17
3
154 Views
Last Modified: 2012-05-11
I have the following piece of code where strError_text1 and strError_2 changes depending on what data is read from a file, but added it statically here to show what error I am getting. This is just one part of a nested Ifs, there are three. I will explain after the code.

The error I get is: Argument 'Start' must be greater than zero.

            Dim strError_text1 as String = "Invalid shipment"
            Dim strError_2 as String = "Receipt closed"

            '****************************************************************************************
            'If strError_text1 = "Invalid shipment" And Mid(strError_2, strError_2.Length - 14, 14) = "does not exist" Then
            If strError_text1 = "Invalid shipment" And Mid(strError_2, strError_2.IndexOf("does not exist"), 14) = "does not exist" Then
                'MessageBox.Show(strError_2.Substring(0, 14))
                strSubType = "Shipment error"
                strSubType2 = "Invalid carrier and freight terms"
                strMsgSubject = strCompany + " shipment invalid carrier/freight terms combination error"
                strMsgBody = "The shipment carrier/freight terms combination is invalid, correct the combination, before resubmitting the shipment"
            End If

Open in new window


Actually, this is a little more complicated, you only see one part of a three part If statement:
If
Else
    If
    Else
        If
        End If
    End If
End If

Open in new window


Problem is that the strError_2.Length changes and could be less than the number that is subtracted from it and that throws an error. Do I need to check the length of the strError_2 each time before submitting it to the If statement? I am rewriting this code from Winbatch which did not throw an error on this exception.

Please be as specific as possible and thank you in advance!

Thank you,
Marek
0
Comment
Question by:maredzki
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 35160405
There are many new methods for string manipulation and comparison in .NET. Here is an example of one  = )
If strError_text1 = "Invalid shipment" And strError_2.Contains("does not exist") Then
    'MessageBox.Show(strError_2.Substring(0, 14))
    strSubType = "Shipment error"
    strSubType2 = "Invalid carrier and freight terms"
    strMsgSubject = strCompany + " shipment invalid carrier/freight terms combination error"
    strMsgBody = "The shipment carrier/freight terms combination is invalid, correct the combination, before resubmitting the shipment"
End If

Open in new window

0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 250 total points
ID: 35160434
You can try something like this:

            Dim strError_text1 as String = "Invalid shipment"
            Dim strError_2 as String = "Receipt closed"

            '****************************************************************************************
            'If strError_text1 = "Invalid shipment" And Mid(strError_2, strError_2.Length - 14, 14) = "does not exist" Then
            If strError_text1 = "Invalid shipment" AndAlso strError_2.Length > 14 AndAlso Mid(strError_2, strError_2.IndexOf("does not exist"), 14) = "does not exist" Then
                'MessageBox.Show(strError_2.Substring(0, 14))
                strSubType = "Shipment error"
                strSubType2 = "Invalid carrier and freight terms"
                strMsgSubject = strCompany + " shipment invalid carrier/freight terms combination error"
                strMsgBody = "The shipment carrier/freight terms combination is invalid, correct the combination, before resubmitting the shipment"
            End If

Open in new window


More about the AndAlso operator:
http://msdn.microsoft.com/en-us/library/cb8x3kfz%28VS.71%29.aspx
0
 
LVL 2

Author Comment

by:maredzki
ID: 35160781
Thanks guys! I've ended up using a combination of both of your suggestions!

Cheers!
Marek
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

10 Experts available now in Live!

Get 1:1 Help Now