vb.net putting string's substring values into an array

ouch_mybrain_
ouch_mybrain_ used Ask the Experts™
on
Hello Experts,

First of all I am a beginner, I have been actively learning to code for the second day and I like to think im picking it up quickly, if not for the odd stumbling block.

Can someone please help me complete the following code. I am trying to create a small program that accepts a string input and then puts the characters in that string into an array. The code is triggered by the button "Button1" on the form, there is also a text box that allows you to enter the string in the first place.

I know the logic as to how I want it to work (see below), but i'm having trouble putting it into code. Can anyone help or provide advice.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click

        Dim array As New ArrayList
        array.Clear()

        Dim inputword As String
        inputword = txtBoxGuess.Text

        Dim inputwordlength As Integer
        inputwordlength = inputword.Length

        Dim loopcount As Integer

        '[start loop]
        '
        'add next unprocessed character of string "inputword" to first space allocation in array ("inputword.substring(????)")
        '
        'increment integer "loopcount" by 1
        '
        'stop looping when integer "inputword.length" is the same value as integer "loopcount"
        '
        '[endloop]


    End Sub
End Class

Open in new window


Many Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You should be able to use inputword.ToArray() ;

But strings are already arrays of characters. You should also be able to use inputword.Substring() to get at the characters.

In Visual Studio, if you type inputword, then a dot, Intellisense should give you all sorts of functions that are available to be used on the object.
Commented:
This is the closest reasonable version of the above.  With the For Each you don't need a loop counter etc.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)      Handles btnGuess.Click

        Dim array As New ArrayList
        Dim inputword As String
        inputword = txtBoxGuess.Text

       For Each element As Char In inputword
          array.Add(element)
      Next
 End Sub
End Class

However better practice if you are using .NET 2.0 or higher (which you almost certainly are), is to use Generic List of Chars rather than an ArrayList.  It will be more efficient and provide compile time type safety.  That would look like this:

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)      Handles btnGuess.Click

        Dim charList As New List(Of Char)
        Dim inputword As String
        inputword = txtBoxGuess.Text

       For Each element As Char In inputword
          charList .Add(element)
      Next
 End Sub
End Class

Commented:
Certainly mwheeler1982's option above is the most efficient.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
Thank you both, for giving me some good info and lots to think about .... very interested about these "elements", puts the confusion I had about loops to rest.

Thanks again
    Sub Main()

        Dim myString As String = "This is my string!"
        Dim i As Integer = 0

        'Access by index
        'Note that a 5 character string runs from index of 0 - 4, indexes generally start at 0 for first item in an array
        'so we subtract 1 from length for upper bound
        While (i <= (myString.Length - 1))

            Console.WriteLine("myString({0}) = {1}", i, myString(i))
            i = i + 1
        End While

        'Now lets do it the easy way
        'A string is an array of characters
        For Each c As Char In myString
            Console.WriteLine(c)
        Next

        Console.ReadLine()
    End Sub

Open in new window


Both of these examples work based off the fact that a string IS an array of characters.  That being said, when at all possible use the provided string functions and I would say .NET provided functions in general when dealing with arrays.  They will help protect you from run-time errors resulting from accessing out of the bounds of the array.  I remember when I was first learning C++ in school that strings could be a pain to deal with thanks to out of bounds access due to a slip in hand coded iteration.

The first example of doing it is based off directly looking up the character based off the index.  This appears to be where you were headed in your code above.

The second example is using the 'For Each' statement.  I would use this over hand coding a counter and iterating through by hand in most all cases.

It allows you to declare a temporary variable, in this case 'c', and repeats the behavior for each instance of a character in the collection of characters.

Also, if you do need to do some array manipulation with strongly typed objects, I'd suggest looking into 'List(of T)' as soon as possible.  That thing is glorious for simplifying lists of just about anything.
Basically mirror posted what Jarnesrh said, that's what happens when I do 6 things at once while typing!

Author

Commented:
Thanks Memccullock - very helpful comments. I will know going forward about strings pretty much being arrays in themselves.

I'm actually trying to build my own "hang man" game and I needed a way to interrogate characters in a word so I can then put logic in to say whether the player loses a life or not. Not really that interesting but thought it would be a good way to develop my vb skills - and it's doing the trick so far :)

Thanks again everyone

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial