Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. exception error caused by reading text file

exception error caused by reading text file

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Latheesan
    wrote on last edited by
    #1

    Im using something called syntax high lighting textbox library in one of my project. To be able to easily highlight syntax, i wrote this lil code: /* Syntax Highlighting Keywords [ BLUE ] */ TextReader syntax_blue = new StreamReader("synHighLib_DB/blue.txt"); while (syntax_blue.ReadLine() != null) { shtb.HighlightDescriptors.Add(new HighlightDescriptor(syntax_blue.ReadLine(), Color.Blue, null, DescriptorType.Word, DescriptorRecognition.WholeWord, true)); } syntax_blue.Close(); It compiles properly, but when i start typing into the text box, i get the unhandled exception error. Im 100% sure this is the code that causing the error, because, when i remove it, it works fine...

    T 1 Reply Last reply
    0
    • L Latheesan

      Im using something called syntax high lighting textbox library in one of my project. To be able to easily highlight syntax, i wrote this lil code: /* Syntax Highlighting Keywords [ BLUE ] */ TextReader syntax_blue = new StreamReader("synHighLib_DB/blue.txt"); while (syntax_blue.ReadLine() != null) { shtb.HighlightDescriptors.Add(new HighlightDescriptor(syntax_blue.ReadLine(), Color.Blue, null, DescriptorType.Word, DescriptorRecognition.WholeWord, true)); } syntax_blue.Close(); It compiles properly, but when i start typing into the text box, i get the unhandled exception error. Im 100% sure this is the code that causing the error, because, when i remove it, it works fine...

      T Offline
      T Offline
      turbochimp
      wrote on last edited by
      #2

      Posting the exception you're receiving would be very helpful, but it looks like you're calling ReadLine twice without any assurance that you have two (or a multiple of two) lines to read. Readline is going to advance the cursor each time it's called. Additionally, if the code above is inside an error handler, I would point out that it might not be the best choice to open and parse a text file every time a key is pressed. Finally, a TextReader is a disposable type, so I would recommend either inserting a Try...Finally block or, better, a using (TextReader syntax_blue = ...) block instead.

      The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups