Copying one array element to another array
-
I have written a code to read a text box character by character and copy the characters into another array. As soon as the space character occurs the process should stop. the Program is giving argument null exception at runtime. Any solutions. Here is the code. Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click Dim objreader As New System.IO.StreamReader(file_name.Text) TextBox1.Text = objreader.ReadLine TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf Dim myArray() As Char Dim myArray2() As Char myArray = Me.TextBox1.Text.ToCharArray For i As Integer = 1 To 70 If myArray(i) <> " " Then Else Array.Copy(myArray, myArray2, i) End If Next End Sub
-
I have written a code to read a text box character by character and copy the characters into another array. As soon as the space character occurs the process should stop. the Program is giving argument null exception at runtime. Any solutions. Here is the code. Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click Dim objreader As New System.IO.StreamReader(file_name.Text) TextBox1.Text = objreader.ReadLine TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf Dim myArray() As Char Dim myArray2() As Char myArray = Me.TextBox1.Text.ToCharArray For i As Integer = 1 To 70 If myArray(i) <> " " Then Else Array.Copy(myArray, myArray2, i) End If Next End Sub
ArgumentNullException
means one of the arguments is null ("Nothing" in your lingo).Dim myArray2() As Char
is not allocating any memory, it only tells the compiler myArray2 is going to refer to some char array, however you (or something you call) needs to pay for the memory (as Me.TextBox1.Text.ToCharArray did for the other array reference). Try theNew
keyword. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
I have written a code to read a text box character by character and copy the characters into another array. As soon as the space character occurs the process should stop. the Program is giving argument null exception at runtime. Any solutions. Here is the code. Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click Dim objreader As New System.IO.StreamReader(file_name.Text) TextBox1.Text = objreader.ReadLine TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf Dim myArray() As Char Dim myArray2() As Char myArray = Me.TextBox1.Text.ToCharArray For i As Integer = 1 To 70 If myArray(i) <> " " Then Else Array.Copy(myArray, myArray2, i) End If Next End Sub
See the code below, this example using a textbox and a button for the example. The code below is in the button click event;
Dim out As List(Of Char) = New List(Of Char)
If Not IsNothing(TextBox1.Text) Then
For Each item As Char In TextBox1.Text.ToCharArray
If item <> Chr(32) Then
out.Add(item)
Debug.WriteLine(item.ToString)
Else
Debug.WriteLine("Space found......stopping.")
Exit For
End IfNext
End If
If out.Count > 0 Then
Debug.WriteLine("Out Array Contains: " + out.Count.ToString + " items.")Dim outString As New System.Text.StringBuilder outString.Append("Out contains: ") For Each item As Char In out outString.Append(item.ToString) Next Debug.WriteLine(outString.ToString)
Else
Debug.WriteLine("No items in output array")
End IfDave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject