I am facing a problem with String.CopyTo() method.
I am trying to the copy the value from string to char array using String.CopyTo() method.
Here’s my code
Dim strString As String = "Hello World!"
Dim strCopy(12) As Char
strString.CopyTo(0, strCopy, 0, 12)
For Each ch As Char In strCopy
Console.Write(ch)
Next
Can any one point me in the right direction ?
Thanks.
Edit : I get the this error at runtime.
ArgumentOutOfRangeException
Index and count must refer to a location within the string.
Parameter name: sourceIndex
From the documentation you get ArgumentOutOfRangeException when:
The first case isn’t true as these values are zero or positive.
So it must be that the count is too great for the destination array. Rather than hard code the length do something like this: