Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Substring() VS Mid() funtion in C# VS VB

Hi,
Here is confusion between C# and VB.NET string function. I am getting a error shownw below in C# CODE. This code has been taken from equlaient vb code written in ASP page. Please error

*******************************
Length cannot be less than zero.
Parameter name: length
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length

***********************


C# CODE

 private string zippart(string ZipNum, string thePart)
    {
        string ZipPart = string.Empty;
        int delimPos1 = 0;
        int delimPos2 = 0;

        if (ZipNum.Length == 0)
        {
            ZipPart = "";
            return ZipPart;
        }

        if (ZipNum.IndexOf("*") > 0)
            delimPos1 = ZipNum.IndexOf("*");
        else if (ZipNum.IndexOf("-") > 0)
            delimPos1 = ZipNum.IndexOf("-");
        else if (ZipNum.IndexOf(" ") > 0)
            delimPos1 = ZipNum.IndexOf(" ");
        //Deepak
        else
            delimPos1 = 0;

        delimPos2 = ZipNum.IndexOf("*", delimPos1 + 1);

        switch (thePart)
        {
            case "zip":
                if (delimPos1 > 0 && delimPos1 < 5)
                    ZipPart = ZipNum.Substring(0, delimPos1 - 1);
                else if (delimPos1 > 5)
                    ZipPart = ZipNum.Substring(0, 5);
                else
                    ZipPart = ZipNum.Trim();
                break;
            case "+4":
                if (ZipNum.Length != delimPos1)
                    if (delimPos1 == 0)
                        ZipPart = "";
                    else if (delimPos2 == 0)
                        ZipPart = ZipNum.Substring(delimPos1 + 1, 4);
                    else
                        ZipPart = ZipNum.Substring(delimPos1 + 1, delimPos2 - delimPos1 - 1);

                break;
            default:
                ZipPart = "";
                break;

        }

        return ZipPart;
    }

VB CODE IN ASP FILE

Function ZipPart(ZipNum, thePart)
      ' input: ZipNum zip-ext
      '             thePart which piece you want
      ' output:zip If thePart='zip'
      '             ext If thePart='+4'      
      
      On Error Resume Next
      Dim delimPos1, delimPos2
      Dim length1, length2

      If Len(Trim(ZipNum)) = 0 Then
            ZipPart = ""
            Exit Function
      End If

      'determine the delimiter type
      If InStr(1, ZipNum, "*") > 0 Then
            delimPos1 = InStr(1, ZipNum, "*")
      ElseIf InStr(1, ZipNum, "-") > 0 Then
            delimPos1 = InStr(1, ZipNum, "-")
      ElseIf InStr(1, ZipNum, " ") > 0 Then
            delimPos1 = InStr(1, ZipNum, " ")
      Else
            delimPos1 = 0
      End If
                  
      delimPos2 = InStr(delimPos1 + 1, ZipNum, "*")

      Select Case thePart
            Case "zip"
                  If delimPos1 > 0 And delimPos1 < 5 Then
                        ZipPart = Left(ZipNum, delimPos1 - 1)
                  ElseIf delimPos1 > 5 Then
                        ZipPart = Left(Trim(ZipNum), 5)
                  Else
                        ZipPart = Trim(zipnum)
                  End If
            Case "+4"
                  If Len(ZipNum) <> delimPos1 Then
                        If delimPos1 = 0 Then
                              ZipPart = ""
                        ElseIf delimPos2 = 0 Then
                              ZipPart = Mid(ZipNum, delimPos1 + 1, 4)
                        Else
                              ZipPart = Mid(ZipNum, delimPos1 + 1, delimPos2 - delimPos1 - 1)
                        End If
                  Else
                        ZipPart = ""
                  End If
            Case Else
                  ZipPart = ""
      End Select
End Function



ERROR COMES IN C# CODE IN LAST LINE  I.E.              ZipPart = ZipNum.Substring(delimPos1 + 1, delimPos2 - delimPos1 - 1);

This fucntion is called as shown below:->

 zip.Text = zippart(zip.Text.ToString(), "zip");
 zipExt.Text = zippart(zip.Text.ToString(), "+4");

Input value is  zipText = 3434-3737

Please help ASAP.

Thanks
0
krdeepak123
Asked:
krdeepak123
  • 3
1 Solution
 
MaxOvrdrv2Commented:
problem is here:
delimPos2 = InStr(delimPos1 + 1, ZipNum, "*")

i believe instr returns -1 if not found... check for it.
0
 
MaxOvrdrv2Commented:
and also, if i you want a faster, simpler version of your zippart function, let me know... easily done.
0
 
Paul JacksonCommented:
The problem is that the call to IndexOf returns -1 if the string is not found whereas in VB Instr would return 0.
So when an * doesn't exist in delimPos2 this line : delimPos2= ZipNum.IndexOf("*", delimPos1 + 1); is setting delimPOs2 to -1.

So in the case statement check for -1 rather than 0 :

 switch (thePart)
        {
            case "zip":
                if (delimPos1 > 0 && delimPos1 < 5)
                    ZipPart = ZipNum.Substring(0, delimPos1 - 1);
                else if (delimPos1 > 5)
                    ZipPart = ZipNum.Substring(0, 5);
                else
                    ZipPart = ZipNum.Trim();
                break;
            case "+4":
                if (ZipNum.Length != delimPos1)
                    if (delimPos1 == 0)
                        ZipPart = "";
                    else if (delimPos2 == -1)
                        ZipPart = ZipNum.Substring(delimPos1 + 1, 4);
                    else
                        ZipPart = ZipNum.Substring(delimPos1 + 1, delimPos2 - delimPos1 - 1);

                break;
            default:
                ZipPart = "";
                break;

        }

                 
0
 
krdeepak123Author Commented:
Thanks .....Lovely
0
 
MaxOvrdrv2Commented:
looks a lot like what i said in my first post above... but i didn't provide the code... but no problems.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now