a quick (and easy) question

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

Is this valid:

string[] allSwitchValues = readSwitches.ReadToEnd().Split('|');

I have no way to test it just yet (although it didn't seem to create
compile errors). Originally I had it divided into two variables on two
lines, but it seems like I only need the one variable, assuming I can't
combine the ReadToEnd and Split methods like so.
 
I wudnt say no in conisdering
readSwitches.ReadToEnd()
cud possibly return null which causes a run time exception.

however (i wud like to do it like the following)
string str = readSwitches.ReadToEnd();
if (str == null) {
//report error or do something
//return ???
}
string[] allSwitchValues = str.Split('|');


regards
 
Ashura said:
I wudnt say no in conisdering
readSwitches.ReadToEnd()
cud possibly return null which causes a run time exception.

however (i wud like to do it like the following)
string str = readSwitches.ReadToEnd();
if (str == null) {
//report error or do something
//return ???
}
string[] allSwitchValues = str.Split('|');

I don't think StreamReader *can* return null, actually. From the
documentation:

<quote>
If the current position is at the end of the stream, returns the empty
string("").
</quote>
 
John Salerno said:
Is this valid:

string[] allSwitchValues = readSwitches.ReadToEnd().Split('|');

I have no way to test it just yet (although it didn't seem to create
compile errors). Originally I had it divided into two variables on two
lines, but it seems like I only need the one variable, assuming I can't
combine the ReadToEnd and Split methods like so.

Yes, you can do the above - that should be fine, at least in terms of
syntax. (Are you happy for it not to do anything with line breaks?)
 
Jon said:
John Salerno said:
Is this valid:

string[] allSwitchValues = readSwitches.ReadToEnd().Split('|');

I have no way to test it just yet (although it didn't seem to create
compile errors). Originally I had it divided into two variables on two
lines, but it seems like I only need the one variable, assuming I can't
combine the ReadToEnd and Split methods like so.


Yes, you can do the above - that should be fine, at least in terms of
syntax. (Are you happy for it not to do anything with line breaks?)

Ah, what a smart question. I did notice that every 20th character in the
array was stored as "\r\n1" (or "\r\n0") and I thought maybe this was
just a result of having used a linebreak. So I put it all on one line to
fix this. If I go back to the original way of having this code on two
lines with two variables, would it not do this with the linebreaks?

Also, I noticed that because the file ended with a "|", the array had as
the last character an empty string, I guess because it was reading what
was to the right of that pipe character (which was nothing), so I just
took out that last pipe. Is there a way to fix that too, while leaving
in the pipe character?
 
John Salerno said:
Ah, what a smart question. I did notice that every 20th character in the
array was stored as "\r\n1" (or "\r\n0") and I thought maybe this was
just a result of having used a linebreak. So I put it all on one line to
fix this. If I go back to the original way of having this code on two
lines with two variables, would it not do this with the linebreaks?

No. Putting it into two lines makes no odds at all. You could read the
file a line at a time, of course - or replace all the "\r\n" with ""
after reading it all.
Also, I noticed that because the file ended with a "|", the array had as
the last character an empty string, I guess because it was reading what
was to the right of that pipe character (which was nothing), so I just
took out that last pipe. Is there a way to fix that too, while leaving
in the pipe character?

Just ignore the last returned element.
 
Back
Top