Array data types issue
-
I want to read a sentence from a textbox characterwise and as soon as the space character comes the prevoiusly occured characters are to put in a label. I am having problems in array data types. Here is the code. Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click Dim count As Integer = 0 Dim objreader As New System.IO.StreamReader(file_name.Text) TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf Dim myArray1() As Char = TextBox1.Text.ToCharArray() Dim myArray2 As List(Of Char) = New List(Of Char) For Each c As Char In myArray1 If c <> " " Then myArray2.Add(c) Else id.Text = myArray2() ' here is the issue Exit For End If Next End Sub
-
I want to read a sentence from a textbox characterwise and as soon as the space character comes the prevoiusly occured characters are to put in a label. I am having problems in array data types. Here is the code. Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click Dim count As Integer = 0 Dim objreader As New System.IO.StreamReader(file_name.Text) TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf Dim myArray1() As Char = TextBox1.Text.ToCharArray() Dim myArray2 As List(Of Char) = New List(Of Char) For Each c As Char In myArray1 If c <> " " Then myArray2.Add(c) Else id.Text = myArray2() ' here is the issue Exit For End If Next End Sub
of course that won't work,
id.Text
is a string, and at the momentmyArray2
is a list of characters. In general, you can't assign two different types to one another. Why did you start using a list? a list is not an array (although there is an array deep inside a list). Please don't use confusing variable names,myArray2
is NOT an array at the moment. Anyway, a string isn't an array of characters either, even when you can get at the string's characters one by one, as if it were an array of chars. However there is a string constructor that takes an array of chars. I suggest you step back a bit, and consider choosing, buying and studying a book on VB.NET programming. That will teach you much more in a shorter period of time, and keep you from wasting your time in random experiments while the fundamentals seem missing. As for your current quest, I think something along these linesDim text as string=TextBox1.Text + " "
id.Text=text.SubString(0, text.IndexOf(' '))is all it takes. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
I want to read a sentence from a textbox characterwise and as soon as the space character comes the prevoiusly occured characters are to put in a label. I am having problems in array data types. Here is the code. Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click Dim count As Integer = 0 Dim objreader As New System.IO.StreamReader(file_name.Text) TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf Dim myArray1() As Char = TextBox1.Text.ToCharArray() Dim myArray2 As List(Of Char) = New List(Of Char) For Each c As Char In myArray1 If c <> " " Then myArray2.Add(c) Else id.Text = myArray2() ' here is the issue Exit For End If Next End Sub
Further to my answer to your previous question below, this example adds the label for the output;
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 For Each item As Char In out outString.Append(item.ToString) Next Label1.Text = outString.ToString
Else
Debug.WriteLine("No items in output array")
End IfDave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
-
Further to my answer to your previous question below, this example adds the label for the output;
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 For Each item As Char In out outString.Append(item.ToString) Next Label1.Text = outString.ToString
Else
Debug.WriteLine("No items in output array")
End IfDave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
Are you serious? Every character that would end up in the result would have been copied six times:
For Each item As Char In TextBox1.Text.ToCharArray // that is two copies (string -> char[] -> char)
out.Add(item)
outString.Append(item.ToString) // that is two copies (char -> string -> SB)
Label1.Text = outString.ToStringBTW:
If Not IsNothing(TextBox1.Text) Then
is superfluous, TextBox.Text never returns null, when empty (or not explicitly initialized) it returns an empty string, as in "" :(Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Are you serious? Every character that would end up in the result would have been copied six times:
For Each item As Char In TextBox1.Text.ToCharArray // that is two copies (string -> char[] -> char)
out.Add(item)
outString.Append(item.ToString) // that is two copies (char -> string -> SB)
Label1.Text = outString.ToStringBTW:
If Not IsNothing(TextBox1.Text) Then
is superfluous, TextBox.Text never returns null, when empty (or not explicitly initialized) it returns an empty string, as in "" :(Luc Pattyn [My Articles] Nil Volentibus Arduum
It works, I never said it was efficient! It would have been better just to shift the char straight to the target than through another builder. When I ran the test this morning, without the line char copy failed when empty. I could have just as easily checked the length (which would have been better). Anyway, the post is more to get the OP thinking, not necessarily give him the answer on a plate. :sigh:
Dave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
-
It works, I never said it was efficient! It would have been better just to shift the char straight to the target than through another builder. When I ran the test this morning, without the line char copy failed when empty. I could have just as easily checked the length (which would have been better). Anyway, the post is more to get the OP thinking, not necessarily give him the answer on a plate. :sigh:
Dave Find Me On: Web|Facebook|Twitter|LinkedIn
Folding Stats: Team CodeProject
Hmm. I think your
If out.Count > 0 Then
is superfluous too. When the TextBox looses all its content at once (CTRL/A CTRL/X), the label needs to get cleared. Maybe you needed a coffee shot first... I still prefer my two-line approach, no list, no SB, no loops. :)Luc Pattyn [My Articles] Nil Volentibus Arduum