Solved

String manipulation issue

Posted on 2011-03-17
3
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

690 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