|
-
Jun 17th, 2008, 06:56 AM
#1
Thread Starter
Member
[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
-
Jun 17th, 2008, 07:03 AM
#2
Re: [2008] Smaller code
You'd have to use arrays and loops, read up on them a bit then try to rewrite your code.
-
Jun 17th, 2008, 07:04 AM
#3
Re: [2008] Smaller code
var2 + jou2 are always the same -
vb Code:
var2 = myfile2.ReadLine()
jou2 = myfile.ReadLine()
so you can put them before the if block
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 17th, 2008, 11:07 AM
#4
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
 
-
Jun 17th, 2008, 11:46 AM
#5
Frenzied Member
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 
-
Jun 17th, 2008, 01:23 PM
#6
Thread Starter
Member
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!
-
Jun 17th, 2008, 01:26 PM
#7
Re: [RESOLVED] [2008] Smaller code
I would only recommend changing vbNewLine to Environment.NewLine
-
Jun 17th, 2008, 01:27 PM
#8
Hyperactive Member
Re: [RESOLVED] [2008] Smaller code
smaller code 
vb Code:
Dim Lines As String() = IO.File.ReadAllLines(pathToFile)
For i As Integer = 0 To Lines.GetUpperBound(0)
'Do something with each line here
Next
-
Jun 17th, 2008, 01:27 PM
#9
Re: [RESOLVED] [2008] Smaller code
why not this?
vb Code:
Dim lines() As String = io.file.readalllines(text_file)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 17th, 2008, 01:28 PM
#10
Thread Starter
Member
Re: [RESOLVED] [2008] Smaller code
MaximilianMayrhofer and what is the difference between those two?
-
Jun 17th, 2008, 01:32 PM
#11
Re: [RESOLVED] [2008] Smaller code
Environment.NewLine is .net code and vbNewLine is legacy code
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 17th, 2008, 01:36 PM
#12
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|