|
-
Mar 3rd, 2004, 05:14 AM
#1
Thread Starter
Addicted Member
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
-
Mar 3rd, 2004, 05:29 AM
#2
Fanatic Member
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
-
Mar 3rd, 2004, 06:41 AM
#3
First of all would you rather use a collection or a String Array?
VB Code:
'string array sample
Dim strFiles() as String
'For example we will fill strFiles() in the forms' load event
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strFiles = IO.Directory.GetFiles("c:", "*.*")
'Lets Display the list of files for fun....
For I As Integer = 0 To strFiles.GetUpperBound(0)
MsgBox(strFiles(I))
Next
End Sub
VB Code:
'Example using a collection...
Dim colFiles as New Collection
'Again we will use the form load event...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x = New Collection ' remove all previous data (really not needed here)
Dim strFiles() As String = IO.Directory.GetFiles("c:\", "*.*")
For I As Integer = 0 To strFiles.GetUpperBound(0)
x.Add(strFiles(I))
Next
'now read the files from the collection
For y As Integer = 1 To x.Count
MsgBox(CType(x.Item(y), String))
Next
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
-
Mar 3rd, 2004, 07:35 AM
#4
Thread Starter
Addicted Member
thanks thanks thanks - thats really helped me~~~~!!!
-
Mar 3rd, 2004, 07:49 AM
#5
Thread Starter
Addicted Member
how can i iterate with the "for each" statemant in the array??
-
Mar 3rd, 2004, 09:11 AM
#6
you can only do that with collections
for example this code would replace the "For y as Integer ..." in my sample above
VB Code:
For Each obj As Object In x
MsgBox(CType(obj, String))
Next
or you could be explicit
VB Code:
For Each str As String In x
MsgBox(str)
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
-
Mar 3rd, 2004, 09:22 AM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|