Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Class Form1
    Dim str() As String
    Dim x As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x = 0 To 5
            str(x) = " D"
        Next
    End Sub
End Class


I could have sworn to adding strings to an array in the past through a loop worked and now it just errors out.
Posted
Updated 14-Jan-11 3:55am
v2

You have not defined the array size.
You have declared an array that is then not initialized thus causing errors.

Use
VB
Dim str(4) As String
and you should be fine.
 
Share this answer
 
Comments
Nish Nishant 14-Jan-11 9:55am    
Proposed as answer, voted 5.
Prerak Patel 18-Jan-11 4:00am    
Shouldn't it be 5?? It's VB.Net.
Dim str(5) As String
Abhinav S 18-Jan-11 23:49pm    
Good pint Prerak - but I hope the OP got the general idea.
String is immutable, use StringBuilder

Copied from documentation:
A string is a sequential collection of Unicode characters, typically used to represent text, while a String is a sequential collection of System.Char objects that represents a string. The value of the String is the content of the sequential collection, and the value is immutable.

A String is called immutable because its value cannot be modified once it has been created. Methods that appear to modify a String actually return a new String containing the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class.


Regards
Espen Harlinn
 
Share this answer
 
v2
Another way to do is:

Dim MyList as new list(of string)

For X = 0 to 5
    MyList.add " D"
Next

'Now you can just use MyList.ToArray()


Good luck! :thumbsup:

Erik.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900