Results 1 to 10 of 10

Thread: [HELP] My random code in text file need a little adjustment!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation [HELP] My random code in text file need a little adjustment!

    Hello! glad to talk with you again! you are very effective! !
    ok i have a little problem with a code!
    indeed, my code generate a random file but sometimes i have no file,error or a blank in a mediaplayer or other stuff!
    here is my code:
    Code:
     
                    Dim Randomtaunt As New Random()
                    Dim Tra As IO.TextReader = System.IO.File.OpenText(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt")
                    Dim FileLinest() As String = Split(Tra.ReadToEnd(), vbCrLf)
                    Tra.Close()
                    Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, UBound(FileLinest)))
                    Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
                    Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
                    Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
    i suspect when the computer read my text file it could read a blank line. how must we fix it?

    thank you in advance for your answer!
    Last edited by danzey; Jul 4th, 2020 at 06:48 PM.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: [HELP] My random code in text file need a little adjustment!

    Well, the first thing you need to do is break things into steps as it looks like you just randomly chucked what you found on google into a method.

    Step one: Read the random class. Since the default random is time dependent you will generate the same seeded numbers.

    https://docs.microsoft.com/en-us/dot...ew=netcore-3.1

    step 2: read a text file using file.readalltext https://docs.microsoft.com/en-us/dot...System_String_

    https://docs.microsoft.com/en-us/dot...ew=netcore-3.1

    https://docs.microsoft.com/en-us/dot...veEmptyEntries


    write the first parts then come back

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

    Re: [HELP] My random code in text file need a little adjustment!

    First, declare your random object at form level...

    Code:
    Dim Randomtaunt As New Random()
    Then, be sure MyTAUNT <> ""...

    Code:
    Dim FileLinest() As String = io.file.readalllines(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt").where(function(f) f.Trim <> "").ToArray
    Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, FileLinest.Length))
    Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
    Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
    Me.AxWindowsMediaPlayer5.Ctlcontrols.play()

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP] My random code in text file need a little adjustment!

    ok i will do my best! thank you for hins.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: [HELP] My random code in text file need a little adjustment!

    My code checks the line isn't empty

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP] My random code in text file need a little adjustment!

    Quote Originally Posted by .paul. View Post
    My code checks the line isn't empty
    BIG THANK YOU PAUL! YOU helped me so much! you are a real gentleman! you code is perfect!
    my regards

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: [HELP] My random code in text file need a little adjustment!

    If your issue is resolved then please use the Thread Tools menu to mark the thread Resolved. That will save anyone else opening the thread and reading six posts, only to find that there's no more help required.

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: [HELP] My random code in text file need a little adjustment!

    Quote Originally Posted by danzey View Post
    BIG THANK YOU PAUL! YOU helped me so much! you are a real gentleman! you code is perfect!
    my regards
    Its not perfect and I guess you didnt read a single thing I posted and just accepted code instead of wanting to learn.

    IsNullOrWhiteSpace

    Trim the string is iterated through from each end.

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

    Re: [HELP] My random code in text file need a little adjustment!

    Quote Originally Posted by ident View Post
    Its not perfect and I guess you didnt read a single thing I posted and just accepted code instead of wanting to learn.

    IsNullOrWhiteSpace

    Trim the string is iterated through from each end.
    It reads the text file omitting any empty lines. That's all the OP wanted

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: [HELP] My random code in text file need a little adjustment!

    Quote Originally Posted by .paul. View Post
    First, declare your random object at form level...

    Code:
    Dim Randomtaunt As New Random()
    Then, be sure MyTAUNT <> ""...

    Code:
    Dim FileLinest() As String = io.file.readalllines(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt").where(function(f) f.Trim <> "").ToArray
    Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, FileLinest.Length))
    Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
    Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
    Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
    That code should call File.ReadLines rather than File.ReadAllLines. The latter will read the whole file into an array first, then process that array and create another array, while the former will process each line as it's read from the file and only create one array when it's done. Also, while I realise it was copied from the OP, the path building should be done using Path.Combine. It's the same folder path but different file paths so the folder path should be built once and then that used to build each file path:
    vb.net Code:
    1. Dim folderPath = Path.Combine(Application.StartupPath, "movies\TAUNTVICTORY")
    2. Dim filePath = Path.Combine(folderPath, Label23.Text & ".txt")

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