Results 1 to 12 of 12

Thread: [RESOLVED] [2008] Smaller code

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Location
    Latvia (Small country near Baltic Sea)
    Posts
    54

    Resolved [RESOLVED] [2008] Smaller code

    Please someone help me to make this code smaller.
    Imagine how much code I would have when var1 = 100.
    Please!

    Code:
        Private Sub BackgroundWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
            Dim myfile = My.Computer.FileSystem.OpenTextFieldParser(tempdir & updatefile_list)
            Dim myfile2 = My.Computer.FileSystem.OpenTextFieldParser(tempdir & updatelist_explorer)
            var1 = myfile.ReadLine()
            If var1 = 1 Then
                var2 = myfile2.ReadLine()
                jou2 = myfile.ReadLine()
            ElseIf var1 = 2 Then
                var2 = myfile2.ReadLine()
                var3 = myfile2.ReadLine()
                jou2 = myfile.ReadLine()
                jou3 = myfile.ReadLine()
            ElseIf var1 = 3 Then
                var2 = myfile2.ReadLine()
                var3 = myfile2.ReadLine()
                var4 = myfile2.ReadLine()
                jou2 = myfile.ReadLine()
                jou3 = myfile.ReadLine()
                jou4 = myfile.ReadLine()
                ' and so on
            End If
        End Sub

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Smaller code

    You'd have to use arrays and loops, read up on them a bit then try to rewrite your code.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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

    Re: [2008] Smaller code

    var2 + jou2 are always the same -

    vb Code:
    1. var2 = myfile2.ReadLine()
    2. jou2 = myfile.ReadLine()

    so you can put them before the if block

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2008] Smaller code

    Rather than Var2 and Jou2, you need to be using two List (of T) objects, where T is the datatype for Var2 and Jou2. Then you would read the data into Var1, and do a loop such as:

    Code:
    For x=0 to Var1
     varList.Add(myFile2.ReadLine())
     jouList.Add(myFile.ReadLine())
    Next
    Of course, readLine may not return a value of the proper type for var or jou, in which case you will have to cast them, but that would be the rough loop you would need.
    My usual boring signature: Nothing

  5. #5
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Smaller code

    Also, since all you are using is ReadLine, using TextFieldParser makes no sense. What you should be using is a StreamReader, if not ReadAllLines. Also, read up on using arrays and lists, there are some nice tutorials at http://www.homeandlearn.co.uk , not sure if they teach using lists though.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2007
    Location
    Latvia (Small country near Baltic Sea)
    Posts
    54

    Re: [2008] Smaller code

    Thank you all...but oh well, I searched in the forum a bit and here is the new code:

    Code:
        Private Sub BackgroundWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
            ' RAT = Read All Text
            RAT = My.Computer.FileSystem.ReadAllText(text_file)
            Dim lines() As String = RAT.Split(New String() {vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
        End Sub
    Big thanks to all the VBForums members!

  7. #7
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [RESOLVED] [2008] Smaller code

    I would only recommend changing vbNewLine to Environment.NewLine

  8. #8
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [RESOLVED] [2008] Smaller code

    smaller code

    vb Code:
    1. Dim Lines As String() = IO.File.ReadAllLines(pathToFile)
    2. For i As Integer = 0 To Lines.GetUpperBound(0)
    3.          'Do something with each line here  
    4. Next

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

    Re: [RESOLVED] [2008] Smaller code

    why not this?

    vb Code:
    1. Dim lines() As String = io.file.readalllines(text_file)

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2007
    Location
    Latvia (Small country near Baltic Sea)
    Posts
    54

    Re: [RESOLVED] [2008] Smaller code

    MaximilianMayrhofer and what is the difference between those two?

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

    Re: [RESOLVED] [2008] Smaller code

    Environment.NewLine is .net code and vbNewLine is legacy code

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2007
    Location
    Latvia (Small country near Baltic Sea)
    Posts
    54

    Re: [RESOLVED] [2008] Smaller code

    Thanks gnaver, but I dont need to do something with each line, I just need to save each line and then use it in something other.
    Thanks a lot .paul.! Even better code, lol, but gnaver's first line of code is almost the same as yours so that means you're both are thinking the same, just gnaver thought about more things what I could do and thanks for that again gnaver! :P Rated both of you!

    Thanks .paul. for the answer!

    Rated MaximilianMayrhofer also! :P

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