Click here to Skip to main content
15,888,293 members
Home / Discussions / C#
   

C#

 
AnswerRe: Text Files Pin
OriginalGriff11-Feb-10 23:50
mveOriginalGriff11-Feb-10 23:50 
GeneralRe: Text Files Pin
muka6612-Feb-10 1:56
muka6612-Feb-10 1:56 
GeneralRe: Text Files Pin
ddecoy12-Feb-10 2:05
ddecoy12-Feb-10 2:05 
GeneralRe: Text Files Pin
muka6612-Feb-10 2:44
muka6612-Feb-10 2:44 
GeneralRe: Text Files Pin
harold aptroot12-Feb-10 2:07
harold aptroot12-Feb-10 2:07 
GeneralRe: Text Files Pin
daveyerwin12-Feb-10 2:22
daveyerwin12-Feb-10 2:22 
GeneralRe: Text Files Pin
muka6612-Feb-10 2:46
muka6612-Feb-10 2:46 
GeneralRe: Text Files Pin
OriginalGriff12-Feb-10 4:58
mveOriginalGriff12-Feb-10 4:58 
I see from later messages that you got it sorted anyway, but there are a comments. Please do not assume I am "getting at you", I'm not - I'm just trying to make your life easier later on!

1) When you post code fragments, use the "code block" widget on the message entry page. This preserves the formatting and makes it easier to read. It doesn't make much difference with a tiny bit of code like yours, but any bigger and it really can improve things. There are people here who won't read tour code unless it is formatted - and they include some people who seriously know what they are doing!

2) Get into the habit of using "too many" curly braces - whenever you can use them do. Particularly in the early days, they make it a lot more obvious where the flow of control is going. If you don't then there is always the temptation to add a new line of code which is outside the scope you intended:
if (condition)
   statement;
becomes:
if (condition)
   statement;
   statement;
and then you are wondering why the second one is always executed!
if (condition)
   {
   statement;
   }
becomes:
if (condition)
   {
   statement;
   statement;
   }
Is a lot more obvious. (And yes, I know VS re-formats code automatically - but it doesn't always.)

3) You don't need the "objStream.Close()" - the using statement Disposes of the file stream, so there is nothing to close!

4) Try to be consistent with your naming: lblerror and lblDispaly can't both be right! Use lblError and lblDisplay instead. Remember that VS will rename variables for you if you ask it nicely...

5) Use names that describe what something does, not what it is: objStream is neither helpful nor accurate! srInputFile is a lot more useful when you come back to it in six months time.

6) Try to declare things in a minimum scope: for example:
StreamReader objStream;
string myfile = Request.QueryString["txtreader"] + ".txt";
string reading = Server.MapPath(myfile);
string[] lines = new string[5]; 
using (objStream = new StreamReader(reading)) 
   {
   for (int i = 0; i < 5; i++)
      {
      if (!objStream.EndOfStream)
         {
         lines[i] = objStream.ReadLine();
         lblDispaly.Text = lines[i];
         }
      }
   }
objStream.Close();
Because objStream is only usable within the using block, declare it in the using block. That way, if you try to use it outside, the compiler will complain and you will realize there is a problem before you can run the code. Catching bugs in the compiler is way faster than at runtime!
string myfile = Request.QueryString["txtreader"] + ".txt";
string reading = Server.MapPath(myfile);
string[] lines = new string[5]; 
using (StreamReader objStream = new StreamReader(reading)) 
   {
   for (int i = 0; i < 5; i++)
      {
      if (!objStream.EndOfStream)
         {
         lines[i] = objStream.ReadLine();
         lblDispaly.Text = lines[i];
         }
      }
   }
objStream.Close();   //COMPILER WILL COMPLAIN HERE - CORRECTLY!


This may seem a lot of criticism, but it's not really - it's just good coding practices (hopefully) explained as to why we do things the way we do! It really does speed things up when projects get more complex, and it's is worth getting right from the start, because that way you don't have "bad habits" to break later on.
If Barbie is so popular, why do you have to buy her friends?

Eagles may soar, but weasels don't get sucked into jet engines.

If at first you don't succeed, destroy all evidence that you tried.

GeneralRe: Text Files Pin
muka6614-Feb-10 22:42
muka6614-Feb-10 22:42 
QuestionLoooping through DataSet with threads but locking Pin
Danpeking11-Feb-10 22:39
Danpeking11-Feb-10 22:39 
AnswerRe: Loooping through DataSet with threads but locking Pin
OriginalGriff11-Feb-10 22:43
mveOriginalGriff11-Feb-10 22:43 
GeneralRe: Loooping through DataSet with threads but locking Pin
Danpeking11-Feb-10 23:38
Danpeking11-Feb-10 23:38 
GeneralRe: Loooping through DataSet with threads but locking Pin
OriginalGriff11-Feb-10 23:42
mveOriginalGriff11-Feb-10 23:42 
GeneralRe: Loooping through DataSet with threads but locking Pin
Danpeking12-Feb-10 5:04
Danpeking12-Feb-10 5:04 
QuestionHow i can refresh CD/DVD Drive ? Pin
Nematjon Rahmanov11-Feb-10 22:30
Nematjon Rahmanov11-Feb-10 22:30 
AnswerRe: How i can refresh CD/DVD Drive ? Pin
Abhinav S11-Feb-10 22:49
Abhinav S11-Feb-10 22:49 
GeneralRe: How i can refresh CD/DVD Drive ? Pin
Nematjon Rahmanov12-Feb-10 0:08
Nematjon Rahmanov12-Feb-10 0:08 
QuestionWhat invoked the getformattedvalue function in datagridviewtextboxcell? Pin
HideIvy11-Feb-10 21:30
HideIvy11-Feb-10 21:30 
AnswerRe: What invoked the getformattedvalue function in datagridviewtextboxcell? Pin
HideIvy11-Feb-10 22:44
HideIvy11-Feb-10 22:44 
Questiondisplay MailMessage Pin
paschka7611-Feb-10 21:02
paschka7611-Feb-10 21:02 
AnswerRe: display MailMessage Pin
AspDotNetDev11-Feb-10 21:51
protectorAspDotNetDev11-Feb-10 21:51 
AnswerRe: display MailMessage Pin
i gr811-Feb-10 22:48
i gr811-Feb-10 22:48 
QuestionI know how to clear textboxs on my form at one time but what if they are bound? Pin
tonyonlinux11-Feb-10 20:28
tonyonlinux11-Feb-10 20:28 
AnswerRe: I know how to clear textboxs on my form at one time but what if they are bound? Pin
AspDotNetDev11-Feb-10 21:13
protectorAspDotNetDev11-Feb-10 21:13 
GeneralRe: I know how to clear textboxs on my form at one time but what if they are bound? Pin
tonyonlinux11-Feb-10 21:19
tonyonlinux11-Feb-10 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.