ERROR: Index and length must refer to a location within the string.

Posted by: umesh-t20 on 8 September 2017, 1:11 pm EST

  • Posted 8 September 2017, 1:11 pm EST

    Hi all,
             I try to convert Vb.net code into c# but i got above error .

    VB.net code :
    Dim n, n0, n1, n2, n3, n4, v1, v2, v3, v4
            n = 1
            n0 = n
            While Mid(xx, n, 1) <> "."
                Application.DoEvents()
                n = n + 1
            End While
            n1 = n
            n = n + 1
            While Mid(xx, n, 1) <> "."
                n = n + 1
            End While
            n2 = n
            n = n + 1
            While Mid(xx, n, 1) <> "."
                Application.DoEvents()
                n = n + 1
            End While

            n3 = n
            n4 = Len(xx)
            MessageBox.Show(n4)
            v1 = Mid(xx, n0, n1 - 1)
            v2 = Mid(xx, n1 + 1, n2 - n1 - 1)
            v3 = Mid(xx, n2 + 1, n3 - n2 - 1)
            v4 = Mid(xx, n3 + 1, n4 - n3)
            v4 = v4 + 1

            If v4 > 255 Then
                v3 = v3 + 1
                v4 = 0
            End If

            If v3 > 255 Then
                v2 = v2 + 1
                v3 = 0
                v4 = 0
            End If

            If v2 > 255 Then
                v1 = v1 + 1
                v2 = 0
                v3 = 0
                v4 = 0
            End If
            Return (v1 & "." & v2 & "." & v3 & "." & v4)

    C#  code :

    int n, n0, n1, n2, n3, n4, v1, v2, v3, v4;
                n = 1;
                n0 = n;

               // while (currip.PadRight(currip.Length + (".").Length).Remove(1, (".").Length).Insert(1, ".").Substring(0, currip.Length) != ".")
                while(currip.Substring(n,1)!=".")
                {
                    Application.DoEvents();
                    n = Convert.ToInt32(n + 1);
                }

                n1 = n;
                n = n + 1;
                while (currip.Substring(n, 1) != ".")
                {
                   n =Convert.ToInt32( n + 1);
                }
                n2 = n;
                n = n + 1;
                while (currip.Substring(n, 1) != ".")
                {
                    Application.DoEvents();
                    n = Convert.ToInt32(n + 1);
                }

                n3 = n;
                n4 = Convert.ToInt32(currip.Length);           
                v1 = Convert.ToInt32(currip.Substring(n0, n1-1 ));           
                v2 = Convert.ToInt32(currip.Substring( n1 + 1, n2 - n1 - 1));
                v3 = Convert.ToInt32(currip.Substring(n2 + 1, n3 - n2 - 1));
                v4 = Convert.ToInt32(currip.Substring(n3 + 1, n4 - n3));          //    Error at this line
                v4 = v4 + 1;


                if (v4 > 255)
                {
                    v3 = v3 + 1;
                    v4 = 0;
                }

                if (v3 > 255)
                {
                    v2 = v2 + 1;
                    v3 = 0;
                    v4 = 0;
                }

                if (v2 > 255)
                {
                    v1 = v1 + 1;
                    v2 = 0;
                    v3 = 0;
                    v4 = 0;
                }
                return (v1 + "." + v2 + "." +  v3 + "." + v4);

  • Replied 8 September 2017, 1:11 pm EST

    Hello,


    What does this do with our Spread product? This would probably be better posted to a Microsoft forum. I would suggest debugging both code snipets to make sure the variables are being set to the same thing in each version.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels