dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Copying array list to text file

  1. #1

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Resolved [RESOLVED] Copying array list to text file

    Name:  Copying files.jpg
Views: 260
Size:  23.7 KB

    As you can see I have made an array list with 3 random names. I would like to copy all the names to a file named hi.txt (see image). But as I run this code. The names do not appear on the text file. What am I doing wrong?

    Thank you
    Last edited by tushardevi; Dec 22nd, 2017 at 12:20 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,217

    Re: [I'M STUCK] Copying array list to text file

    Welcome to VBForums

    When you post code please put it as text (rather than a picture) inside code tags so it is displayed in a more readable way - either using the Code/VBCode buttons in the post editor screen (or at the top of the Quick Reply box), or by putting them in manually, like this: [code] code here [/code] Having it as text means we can easily copy+paste if it is useful to do so (and it often is).


    The reason that the names aren't being written to the file is because you aren't doing anything to make that happen... you are displaying the names to the screen, and getting an input from the user - and it is the input from the user that you are writing to the file.

    Instead of Console.WriteLine etc, open the file before the loop, and move the Print line inside the loop - but instead of using n as the data to write, use Names(i)


    Also note that you aren't using an "array list" (which is something different, and shouldn't really be used any more), but an array.

  3. #3

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Re: [I'M STUCK] Copying array list to text file

    Hi si_the_geek

    I would like to apologise for using image instead of text. Also I am a beginner so I did not know the difference between an array and an array list. Anyways, it actually worked!

    Thank you for your help!

  4. #4

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Re: [I'M STUCK] Copying array list to text file

    Module Module1

    Sub Main()
    Dim o As New System.IO.DirectoryInfo("C:\Users\Tushar\Documents")
    Dim myfiles() As System.IO.FileInfo
    myfiles = o.GetFiles()

    FileOpen(1, "C:\Users\Tushar\Desktop\hi.txt", OpenMode.Output)

    For y As Integer = 0 To myfiles.Length - 1
    PrintLine(1, myfiles(y))<-----------------------------------------------ERROR : System.ArgumentException: 'File I/O with type 'FileInfo' is not valid.'

    Next



    End Sub



    End Module

    - Now I have tried to use the same method here. But it is giving me an error. I am trying to browse all the files in a folder and trying to copy the names of every file in that folder in a text file. Is this not possible or am I doing something wrong?

    Thank you

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,217

    Re: [I'M STUCK] Copying array list to text file

    You are on the right lines, but the items in myfiles() have a data type of FileInfo, which is more complicated than a String.

    For simple data types (like String and Integer) there is only one value, but with most data types you have a class with multiple properties, and you need to specify which one to read/write.

    If there was a property called "Dave" that you wanted to use, you would do this:
    Code:
    PrintLine(1, myfiles(y).Dave)
    Obviously Dave isn't the property you want, but you may well be able to guess by typing the dot and seeing what options are listed... or you can take a look at the documentation for the data type, as the Properties list explains what each property is:
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,506

    Re: [I'M STUCK] Copying array list to text file

    Quote Originally Posted by tushardevi View Post
    Hi si_the_geek

    I would like to apologise for using image instead of text. Also I am a beginner so I did not know the difference between an array and an array list. Anyways, it actually worked!

    Thank you for your help!
    It's not a bad thing that you don't know about the ArrayList (no space), as that was really just in the first two version of .NET. As of 2005, the generics were added to .NET, and the ArrayList was no longer used. It's still available, it just shouldn't be used, so it doesn't say anything bad that you don't know about it. What we DO have is arrays, which you are using, and Lists. The List is a generic type, but you can think of it as an array which has methods to Add, Insert, Remove, and RemoveAt, along with other methods. So, an Array is something you use when you know the number of items that will be in it, and that number won't change, while a List is what you use if the collection will grow or shrink over time. The syntax is slightly different, as well. For an array of strings you would write:

    Dim Names(2) As String

    whereas for a List it would look like this:

    Dim Names As New List(of String)
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member tushardevi's Avatar
    Join Date
    Dec 2017
    Location
    Wales
    Posts
    17

    Re: [I'M STUCK] Copying array list to text file

    si_the_geek.

    I figured it out! I just wrote ToString:

    PrintLine(1, myfiles(y).Tostring). And it works!!

    I like the way you dont give out the answer but give hints. That actually helps

    Thank you.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,506

    Re: [RESOLVED] Copying array list to text file

    You never really know about ToString until you try it out. That's a method implemented in class Object, which means it is available for EVERY data type in .NET. However, what it does depends on the data type. The default implementation is just to return the name of the object. Had you gotten back "FileInfo" it wouldn't have impressed you any (it would actually be longer than that). However, they must have overridden the implementation of ToString for the FileInfo object such that it returned what you wanted. That's great, and fairly understandable, since the file name is the most likely thing people want from a FileInfo object, but you should be aware that it doesn't HAVE to work that way. Whoever designed the class gets to decide whether to override .ToString, and what to have it return. Sometimes it's obvious what to return. Other times there isn't anything obvious.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width