Solved

String manipulation issue

Posted on 2011-03-17
3
157 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 75

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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