Results 1 to 10 of 10

Thread: textfile to variable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    67

    textfile to variable

    i have a text file that contains words.. how would i get each line and pass it to a variable.

    here how it goes:
    string variable where each line will be passed and for each line will be passed to a function

  2. #2
    Addicted Member
    Join Date
    Apr 2008
    Posts
    193

    Re: textfile to variable

    Quote Originally Posted by xpinvader View Post
    i have a text file that contains words.. how would i get each line and pass it to a variable.

    here how it goes:
    string variable where each line will be passed and for each line will be passed to a function
    You could use a Stream Reader...

    Code:
    Dim sr As StreamReader = File.OpenText(your file name here)
    Dim ReadBuffer As String = String.Empty
    
     While sr.Peek <> -1
                    ReadBuffer = sr.ReadLine
                    ' Pass the ReadBuffer to your Function.
    
     End While

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    67

    Re: textfile to variable

    thanks for the quick reply..

    how about writing it in the textfile like this:

    string1
    string2
    string3

  4. #4
    New Member
    Join Date
    Dec 2009
    Posts
    10

    Re: textfile to variable

    you can put your text in list
    see:
    Code:
      Dim MyList As New List(Of String)
            Using SR As New IO.StreamReader(you text file)
                Dim strTemp() As String = Split(SR.ReadToEnd, vbCrLf)
                MyList = strTemp.ToList
            End Using

  5. #5
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: textfile to variable

    I could be wrong, but isn't Environment.Newline prefered over vbCrLf?
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  6. #6
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: textfile to variable

    when you say you have a textfile that contains words, can we have a bit more detail on what you mean, ideally a sample of the file data, for instance is it like a dictionary with one word on one line? is it in xml? does it have several words on one line separated by commas?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    67

    Re: textfile to variable

    Quote Originally Posted by Megalith View Post
    when you say you have a textfile that contains words, can we have a bit more detail on what you mean, ideally a sample of the file data, for instance is it like a dictionary with one word on one line? is it in xml? does it have several words on one line separated by commas?
    just one word per line.. i will use it to store all variable names that will be placed in the richtextbox. I have a problem when the user deletes the variable, it is still in the textfile. but which is better the textfile or an xml?

  8. #8
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: textfile to variable

    not much point for xml for this kind of list but i'd store the items in a list as per the example given by Mohammed Assad.

    you will need to save the new edited list when you are done. How many words are in the list btw? might by better to save these like a property bag?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    67

    Re: textfile to variable

    I'm making an auto complete feature of my text editor and I'm looking to store all the variables, function and classes of the source code that will be edited. Most likely the text file will be used during the run time.

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: textfile to variable

    VS2008 code will read your text into a list.

    Code:
            Dim lines As List(Of String) = _
                (From line In IO.File.ReadAllLines("YourFileName.txt")).ToList

Tags for this Thread

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