|
-
Aug 4th, 2008, 12:15 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Can't add to "files()"
I have a string called files() and I need to add items to it for a function to work.
I want to use it in another function but it needs to be modified to look like this:
Code:
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Dim files() As String = OpenFileDialog1.FileNames
ListBox1.Items.Clear()
For x As Integer = 0 To files.GetUpperBound(0)
filesList.Add(files(x))
Next
For x As Integer = 0 To filesList.Count - 1
ListBox1.Items.Add(IO.Path.GetFileName(filesList(x)))
Next
End If
That works but this doesn't:
Code:
Dim files() As String = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"
ListBox1.Items.Clear()
For x As Integer = 0 To files.GetUpperBound(0)
filesList.Add(files(x))
Next
For x As Integer = 0 To filesList.Count - 1
ListBox1.Items.Add(IO.Path.GetFileName(filesList(x)))
Next
.luaproj is an extension I've given to directories("projects" to my application)
That code returns the error: "Type 'String' cannot be converted to '1-dimensional array of String'.
-
Aug 4th, 2008, 12:22 PM
#2
Re: Can't add to "files()"
I assume it is this line that is highlighted when the exception is thrown (it would be useful to tell us that by the way)?
vb Code:
Dim files() As String = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"
What you are doing here is declaring a String as an array by using the brackets () but then telling the program that the array is just one string. Either you want to create an array and want to set the first value in it to the path you are using or you just want to create a single string object and set it.
So either this, for setting it as the first value in an array:
vb Code:
Dim Files() As String
Files(0) = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"
or this for just a normal single string:
vb Code:
Dim Files As String = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"
However, from what you are doing it looks like you might be trying to read in a list of strings from that index.lua file. If thats the case then you need to do something like this:
vb Code:
Dim Files() As String = IO.File.ReadAllLines(FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua")
Of course this is assuming that your index.lua file contains a list of strings on their own seperate lines.
Help at all?
Oh and you might want to consider using the IO.Path.Combine method to create your path instead of just sticking strings together. Its more 'proper'
Last edited by chris128; Aug 4th, 2008 at 12:27 PM.
-
Aug 4th, 2008, 12:33 PM
#3
Thread Starter
Addicted Member
Re: Can't add to "files()"
Thank you for the speedy reply It is the line of code that throws the exception.
Unfortunatley, your code doesn't work. In the code "Files(0)" is underlined in green, and tells me that it has been used before and could throw a null reference exception - at run time this is the case.
I am not trying to read the lines of index.lua into a listbox.
-
Aug 4th, 2008, 12:34 PM
#4
Re: Can't add to "files()"
well is there any reason why it needs to be an array? Why not just use the second example I gave where it is just a single string as it looks to me like that is all you need.
EDIT: After looking at your code again, I have another question - why are you looping through the File() array when you have only tried to add one thing to it? Are you sure you are not trying to read from the file index.lua ?
-
Aug 4th, 2008, 12:37 PM
#5
Re: Can't add to "files()"
to use an array that way you need to dimension it first.
this sets the files() array upperbound to 0
vb Code:
Dim Files(0) As String
Files(0) = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 4th, 2008, 12:39 PM
#6
Re: Can't add to "files()"
Ah yeah, thanks for clarifying that Paul, I'm naff at using arrays without some trial and error :P
However, if you are dimensioning an array with a 0, therefore saying there will be one entry in it, what is the point of having an array that only has one entry in it and then looping through that to extract that value? Why not just have a single string and no loop?
-
Aug 4th, 2008, 12:43 PM
#7
Thread Starter
Addicted Member
Re: Can't add to "files()"
It's really confusing, but it has to work that way to work with the list box.
This might help you see why it needs to work like this: http://www.vbforums.com/showthread.p...14#post3290914.
Thank you guys
-
Aug 4th, 2008, 12:46 PM
#8
Re: [RESOLVED] Can't add to "files()"
Hmm well I dont fully understand but is your problem resolved now by using what paul suggested?
-
Aug 4th, 2008, 12:47 PM
#9
Thread Starter
Addicted Member
Re: [RESOLVED] Can't add to "files()"
Yep
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
|