?
Solved

String manipulation issue

Posted on 2011-03-17
3
Medium Priority
?
168 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 1000 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 1000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

862 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