Results 1 to 9 of 9

Thread: [RESOLVED] Can't add to "files()"

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [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'.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 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:
    1. Dim Files() As String
    2. Files(0) = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"
    or this for just a normal single string:
    vb Code:
    1. 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:
    1. 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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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 ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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:
    1. Dim Files(0) As String
    2. Files(0) = FolderBrowserDialog1.SelectedPath + "\" + TextBox3.Text + ".luaproj\" + "index.lua"

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Can't add to "files()"

    Hmm well I dont fully understand but is your problem resolved now by using what paul suggested?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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
  •  



Click Here to Expand Forum to Full Width