Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !
I wanna declare a string array without specifying the size of string array in vb.net. That is, as new element comes, the array should grow at run time.
How can i do that?
Thanx in advance
Posted

Use a List instead:
VB
Dim myList As New List(Of String)()
myList.Add("Hello")
myList.Add("Goodbye")
Console.WriteLine(myList(1))
If you need it as an array at any time (to pass it to a method for example):
VB
Dim myArray As String() = myList.ToArray()
 
Share this answer
 
Comments
VJ Reddy 23-May-12 5:13am    
Nice answer. 5!
Naveen_143 23-May-12 5:24am    
Hi,
Dim myList As New List(Of String)() is showing an error...Comma,')' , or a valid continuation expected...
OriginalGriff 23-May-12 5:31am    
I just pasted the code in my answer into a VB page and it compiles cleanly - check your code and make sure it is identical. If you can't see a problem, then copy and paste the relevant section of your code with the error message here.
Maciej Los 23-May-12 11:16am    
Good answer, my 5!
005bharat 31-May-13 4:53am    
Great work!
If you wont use Generics, you can do it directly:

VB
Dim s() As String
ReDim Preserve s(UBound(s) + 1)
s(UBound(s)) = "new valor"
 
Share this answer
 
Comments
Naveen_143 12-Jun-12 5:55am    
thanks...
VB
Dim stringArray As String()

' define three strings
Dim value1 As String = "lions"
Dim value2 As String = "tigers"
Dim value3 As String = "bears"

' using the Split() command, dynamically create them as a three element array
stringArray = Split(value1 + "," + value2 + "," + value3)

' define a fourth string
Dim value4 As String = "Oh my!"

' dynamically add to the array
stringArray = Split(Join(stringArray, ",") + ","+ value4, ",";)

' or, you could ...

stringArray = Split("lions" + "," + "tigers" + "," + "bears", ",")
stringArray = Split(Join(stringArray, ",") + "," + "Oh my!", ",")
 
Share this answer
 
Comments
CHill60 17-Jun-13 17:18pm    
It's great that you want to help but this question is a year old and already resolved
Naveen_143 5-Jul-13 4:49am    
thanks for your reply.
VB
Dim webQuery As QueryWebService.Service = New QueryWebService.Service


    Dim queryDetails As String()
    Dim i As Integer = 0

    queryDetails = webQuery.getStudentQuery()

    While i < queryDetails.Length
        txtQuery.Text = txtQuery.Text + " " + queryDetails.GetValue(i)
        i = i + 1
    End While
 
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