Solved

String manipulation issue

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…

830 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