Results 1 to 7 of 7

Thread: dynamic string array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    175

    dynamic string array

    hi all
    i want to create dynamic string array for example
    dim name() as string
    names is a global array

    in other function i want to initiate the array with the Directory.Getfiles method which returns me string array. how can i do that???
    i was told to use collection???
    i dont know the collection object - can someone guide me???

    mabe to declare

    dim names() as new collection
    .
    .
    .

    names.add(Directory.Getfiles(strPath, "*.gif"))
    ..
    but this does not work to me

  2. #2
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    keep ur name() variable Public or Friend so that all classes can access it.

    suppose ur name() variable is in Module1 then

    Module1.name = Directory.Getfiles()

    If in Form1 then

    Form1.name = Directory.Getfiles()

    hope it works

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    First of all would you rather use a collection or a String Array?

    VB Code:
    1. 'string array sample
    2.  
    3. Dim strFiles() as String
    4.  
    5. 'For example we will fill strFiles() in the forms' load event
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.         strFiles = IO.Directory.GetFiles("c:", "*.*")
    10.  
    11.         'Lets Display the list of files for fun....
    12.  
    13.         For I As Integer = 0 To strFiles.GetUpperBound(0)
    14.             MsgBox(strFiles(I))
    15.         Next
    16.  
    17.     End Sub

    VB Code:
    1. 'Example using a collection...
    2.  
    3. Dim colFiles as New Collection
    4.  
    5. 'Again we will use the form load event...
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.         x = New Collection ' remove all previous data (really not needed here)
    10.  
    11.         Dim strFiles() As String = IO.Directory.GetFiles("c:\", "*.*")
    12.  
    13.         For I As Integer = 0 To strFiles.GetUpperBound(0)
    14.             x.Add(strFiles(I))
    15.         Next
    16.  
    17.         'now read the files from the collection
    18.  
    19.         For y As Integer = 1 To x.Count
    20.             MsgBox(CType(x.Item(y), String))
    21.         Next
    22.  
    23.  
    24.     End Sub

    The reason why your collection wasnt working was because when you called Names.Add you passed the the string array
    so the whole array was stored as one Item in the collection

    ~abx
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    175
    thanks thanks thanks - thats really helped me~~~~!!!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    175
    how can i iterate with the "for each" statemant in the array??

  6. #6
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    you can only do that with collections

    for example this code would replace the "For y as Integer ..." in my sample above

    VB Code:
    1. For Each obj As Object In x
    2.             MsgBox(CType(obj, String))
    3.         Next

    or you could be explicit

    VB Code:
    1. For Each str As String In x
    2.             MsgBox(str)
    3.         Next
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    175
    thanks alllllllooooooooot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width