playing with arrays

VBnetster

Active member
Joined
Feb 15, 2008
Messages
32
Programming Experience
Beginner
Hi sup? Just got out VB.net 2008 pretty much the first time i've touched this stuff. Anywayz, I know that an 'int' cant be converted to a 'string' (in most cases) but the following code will show you what im trying to do, i hope:

VB.NET:
        Dim Arraystr(0) As String
        Dim txt As String = "hello"
        Dim i As Integer

        For i = 1 To Len(txt)

            Arraystr(i) = Chr(Mid(txt, i, 1))
            ReDim Preserve Arraystr(i)

        Next

I know theres other ways around this, but im doing it this way just for the purpose of learning Redim.

coolio, thanks.
 
well i do know that in VS 2008 you do not need to declare the For loop variable

i understand what your trying to do.
but you can try this, i know it works every with different string lengths

make a form with 1 textbox 1 button and a listbox but do not rename them
VB.NET:
Public Class Form1
    Dim Arraystr(0) As String
    Dim txt As String

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

        '' if the textbox is empty then exit the sub
        If TextBox1.Text = "" Then Exit Sub

        '' make txt the same as the text in textbox1
        txt = TextBox1.Text

        '' loop from 1 to the length of txt string
        For i = 1 To Len(txt)

            '' ReDim Arraystr to the value of i
            ReDim Preserve Arraystr(i)

            '' Arraystr is equal to a charcter of the string
            Arraystr(i) = Mid(txt, i, 1)

            '' add the current item to listbox1
            '' this is just for a visual representation
            ListBox1.Items.Add(Arraystr(i))

            '' Loop
        Next
    End Sub
End Class

or download this project. hopefully that helps you.
 

Attachments

  • ReDim.zip
    59.9 KB · Views: 20
Back
Top