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,<br>         I try to convert Vb.net code into c# but i got above error .<br><br>VB.net code :<br>Dim n, n0, n1, n2, n3, n4, v1, v2, v3, v4<br>        n = 1<br>        n0 = n<br>        While Mid(xx, n, 1) <> "."<br>            Application.DoEvents()<br>            n = n + 1<br>        End While<br>        n1 = n<br>        n = n + 1<br>        While Mid(xx, n, 1) <> "."<br>            n = n + 1<br>        End While<br>        n2 = n<br>        n = n + 1<br>        While Mid(xx, n, 1) <> "."<br>            Application.DoEvents()<br>            n = n + 1<br>        End While<br><br>        n3 = n<br>        n4 = Len(xx)<br>        MessageBox.Show(n4)<br>        v1 = Mid(xx, n0, n1 - 1)<br>        v2 = Mid(xx, n1 + 1, n2 - n1 - 1)<br>        v3 = Mid(xx, n2 + 1, n3 - n2 - 1)<br>        v4 = Mid(xx, n3 + 1, n4 - n3)<br>        v4 = v4 + 1<br><br>        If v4 > 255 Then<br>            v3 = v3 + 1<br>            v4 = 0<br>        End If<br><br>        If v3 > 255 Then<br>            v2 = v2 + 1<br>            v3 = 0<br>            v4 = 0<br>        End If<br><br>        If v2 > 255 Then<br>            v1 = v1 + 1<br>            v2 = 0<br>            v3 = 0<br>            v4 = 0<br>        End If<br>        Return (v1 & "." & v2 & "." & v3 & "." & v4)<br><br>C#  code :<br><br>int n, n0, n1, n2, n3, n4, v1, v2, v3, v4;<br>            n = 1;<br>            n0 = n;<br><br>           // while (currip.PadRight(currip.Length + (".").Length).Remove(1, (".").Length).Insert(1, ".").Substring(0, currip.Length) != ".")<br>            while(currip.Substring(n,1)!=".")<br>            {<br>                Application.DoEvents();<br>                n = Convert.ToInt32(n + 1);<br>            }<br><br>            n1 = n;<br>            n = n + 1;<br>            while (currip.Substring(n, 1) != ".")<br>            {<br>               n =Convert.ToInt32( n + 1);<br>            }<br>            n2 = n;<br>            n = n + 1;<br>            while (currip.Substring(n, 1) != ".")<br>            {<br>                Application.DoEvents();<br>                n = Convert.ToInt32(n + 1);<br>            }<br><br>            n3 = n;<br>            n4 = Convert.ToInt32(currip.Length);            <br>            v1 = Convert.ToInt32(currip.Substring(n0, n1-1 ));            <br>            v2 = Convert.ToInt32(currip.Substring( n1 + 1, n2 - n1 - 1));<br>            v3 = Convert.ToInt32(currip.Substring(n2 + 1, n3 - n2 - 1));<br>            <b>v4 = Convert.ToInt32(currip.Substring(n3 + 1, n4 - n3));          //    Error at this line </b><br>            v4 = v4 + 1;<br><br><br>            if (v4 > 255)<br>            {<br>                v3 = v3 + 1;<br>                v4 = 0;<br>            }<br><br>            if (v3 > 255)<br>            {<br>                v2 = v2 + 1;<br>                v3 = 0;<br>                v4 = 0;<br>            }<br><br>            if (v2 > 255)<br>            {<br>                v1 = v1 + 1;<br>                v2 = 0;<br>                v3 = 0;<br>                v4 = 0;<br>            }<br>            return (v1 + "." + v2 + "." +  v3 + "." + v4);<br><br>
  • Replied 8 September 2017, 1:11 pm EST

    <P>Hello,</P>
    <P>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.</P>
Need extra support?

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

Learn More

Forum Channels