Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Array data types issue

Array data types issue

Scheduled Pinned Locked Moved Visual Basic
data-structureshelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Razanust
    wrote on last edited by
    #1

    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

    L D 2 Replies Last reply
    0
    • R Razanust

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      of course that won't work, id.Text is a string, and at the moment myArray2 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 lines

      Dim text as string=TextBox1.Text + " "
      id.Text=text.SubString(0, text.IndexOf(' '))

      is all it takes. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      1 Reply Last reply
      0
      • R Razanust

        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

        D Offline
        D Offline
        DaveAuld
        wrote on last edited by
        #3

        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 If

        Next
        

        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 If

        Dave Find Me On: Web|Facebook|Twitter|LinkedIn


        Folding Stats: Team CodeProject

        L 1 Reply Last reply
        0
        • D DaveAuld

          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 If

          Next
          

          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 If

          Dave Find Me On: Web|Facebook|Twitter|LinkedIn


          Folding Stats: Team CodeProject

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.ToString

          BTW: 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

          D 1 Reply Last reply
          0
          • L Luc Pattyn

            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.ToString

            BTW: 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

            D Offline
            D Offline
            DaveAuld
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • D DaveAuld

              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

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups