Results 1 to 8 of 8

Thread: [RESOLVED] could it be better

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Resolved [RESOLVED] could it be better

    i have changed an old vb6 application to 2005.net and i want to know if this sub (which does work) could be better, by that i mean have i used the right functions ?, are there better(faster) ones ?

    this removes a username and every line under it until it reaches another username.
    VB Code:
    1. Public Sub RemoveUser(ByVal StringUser As String)
    2.         Dim sr As New IO.StreamReader("C:\usernamesinfo.txt")
    3.         Dim sw As New IO.StreamWriter("C:\usernamesinfo2.txt", True)
    4.         Dim Found As Boolean, Lines As String
    5.  
    6.         Do While sr.Peek >= 0
    7.             Lines = sr.ReadLine
    8.  
    9.             If Found = False Then
    10.                 If InStr(Lines, "USERNAME" & StringUser) = 0 Then
    11.                     sw.WriteLine(Lines)
    12.                 Else
    13.                     Found = True
    14.                 End If
    15.             Else
    16.                 If InStr(Lines, "USERNAME") > 0 Then
    17.                     Found = False
    18.                     sw.WriteLine(Lines)
    19.                 End If
    20.             End If
    21.         Loop
    22.         sr.Close()
    23.         sw.Close()
    24.         IO.File.Delete("C:\usernamesinfo.txt")
    25.         IO.File.Move("C:\usernamesinfo2.txt", "C:\usernamesinfo.txt")
    26.  
    27.     End Sub

    thanks.

  2. #2
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: could it be better

    VB Code:
    1. Public Sub RemoveUser(ByVal StringUser As String)
    2.         Dim sr As New IO.StreamReader("C:\usernamesinfo.txt")
    3.         Dim sw As StreamWriter
    4.         Dim al As New ArrayList
    5.         Dim i As Integer
    6.  
    7.         Dim str As String
    8.         Dim arrTemp() As String
    9.         Dim Basket As New ArrayList
    10.  
    11.         str = sr.ReadToEnd
    12.         sr.DiscardBufferedData()
    13.         sr.Close()
    14.         IO.File.Delete("C:\usernamesinfo.txt")
    15.         sw = IO.File.CreateText("C:\usernamesinfo.txt")
    16.  
    17.         arrTemp = str.Split("USERNAME")
    18.  
    19.         For i = 0 To arrTemp.Length - 1
    20.             If arrTemp(i).ToString.StartsWith(StringUser) Then
    21.                 'Do nothing, this effectively removes it
    22.             Else
    23.                 Basket.Add(arrTemp(i).ToString)
    24.             End If
    25.         Next
    26.  
    27.         For i = 0 To Basket.Count - 1
    28.             sw.WriteLine("USERNAME" & Basket(i).ToString)
    29.         Next
    30.         sw.Flush()
    31.         sw.Close()
    32.     End Sub

    Haven't tried it out ....but here it goes..
    If you find my thread helpful, please remember to rate me

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: could it be better

    thanks for your reply. its not exactly what i wanted but that s my fault for not showing a typical example of the file but on the other hand you have shown me a few more functions that i didnt know existed.

    thank you very much.

  4. #4
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: could it be better

    no problems

    could you explain what you are trying to do in greater detail?
    If you find my thread helpful, please remember to rate me

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: could it be better

    dinosaur_uk, note that the String.Split method cannot split on a substring, but only a single character. If you pass a string as the argument with Option Strict turned Off it will be implicitly converted to a character by taking just the first character. If Option Strict is turned On the code will not compile. There is an overload that takes an array of characters, but that splits on every instance of any one of those characters. You would need to use either the Split Runtime function or the Regex.Split method.

    I would recommend against using a second file. Unless the file is really large, I would reas it like this:
    VB Code:
    1. Dim lines As String() = System.Text.RegularExpressions.Regex(myStreamReader.ReadToEnd(), Environment.NewLine)
    If you really must use a second file, you should use the System.IO.Path.GetTempFile method to create a genuine temp file.

    Once you have the lines of the file in the array, you can just loop over those strings and either edit them directly, or write them back to the same file name. Finally, use the String.IndexOf method instead of InStr. InStr is a Runtime function that would implicitly call String.IndexOf anyway, so why add the extar layer?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: could it be better

    Ok ...cheers...didnt know that...
    If you find my thread helpful, please remember to rate me

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: could it be better

    with the suggestions of some functions and methods i have changed it all to this.
    VB Code:
    1. Public Sub RemoveUser(ByVal StringUser As String)
    2.         Dim sa As New ArrayList(IO.File.ReadAllLines("C:\usernamesinfo.txt"))
    3.         Dim start As Int16 = sa.IndexOf("USERNAME" & StringUser)
    4.         Dim i As Int16, counter As Int16
    5.  
    6.         For i = start + 1 To sa.Count - 1
    7.             If sa(i).ToString.StartsWith("USERNAME") Then
    8.                 Exit For
    9.             Else
    10.                 counter += 1
    11.             End If
    12.         Next
    13.  
    14.         sa.RemoveRange(start, counter + 1)
    15.  
    16.         IO.File.WriteAllLines("C:\usernamesinfo.txt", sa.ToArray(GetType(String)))
    17.     End Sub

    thanks for the direction.

  8. #8
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: [RESOLVED] could it be better

    nice
    If you find my thread helpful, please remember to rate me

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