Problem with returning a string from a sub
-
Hi all I have a sub that writes to a textbox in the code. I want to change it so it just returns as a string so i can use it over again for other stuff in the program the current code is this
private void Readlog(string path) { StreamReader SR; string S; SR = File.OpenText(path); S = SR.ReadLine(); while (S != null) { if (S != null) { //this is sloppy but i cant have a trailing /r/n so this is the only //way i can think to get rid of it S = S + "\r\n"; } textBox1.AppendText(S); S = SR.ReadLine(); } SR.Close(); }
what i want to do is change it to something likeprivate string Readlog(string path) { string temp; StreamReader SR; string S ; SR = File.OpenText(path); S = SR.ReadLine(); temp =s while (S != null) { if (S != null) { S = S + "\r\n"; } temp = temp +S; S = SR.ReadLine(); } SR.Close(); return temp; }
The problem is that it always returns a null or just one line of text and i know its probably somthing right in front of me but I can't seem to figure out what it is the idea is that i can use it in something like textbox1.text = readlog(c:\test) orForeach something in something { writelog(merge.txt,readlog(something.filename); }
Thanks for pointing me in the right direction -
Hi all I have a sub that writes to a textbox in the code. I want to change it so it just returns as a string so i can use it over again for other stuff in the program the current code is this
private void Readlog(string path) { StreamReader SR; string S; SR = File.OpenText(path); S = SR.ReadLine(); while (S != null) { if (S != null) { //this is sloppy but i cant have a trailing /r/n so this is the only //way i can think to get rid of it S = S + "\r\n"; } textBox1.AppendText(S); S = SR.ReadLine(); } SR.Close(); }
what i want to do is change it to something likeprivate string Readlog(string path) { string temp; StreamReader SR; string S ; SR = File.OpenText(path); S = SR.ReadLine(); temp =s while (S != null) { if (S != null) { S = S + "\r\n"; } temp = temp +S; S = SR.ReadLine(); } SR.Close(); return temp; }
The problem is that it always returns a null or just one line of text and i know its probably somthing right in front of me but I can't seem to figure out what it is the idea is that i can use it in something like textbox1.text = readlog(c:\test) orForeach something in something { writelog(merge.txt,readlog(something.filename); }
Thanks for pointing me in the right directionC# is case sensative on variable names, so your use of s and S were a problem. Also having an IF statement to check for null inside your while statement that is calready checking for null makes no sense. But I guess you want to make sure that it's not null. Here's my cleanup of your code. I also ignored empty strings, but if you want to keep those, then change the while comparison to be while(temp != null)
private string Readlog(string path)
{
string returnValue = string.Empty;using (StreamReader sr = File.OpenText(path)) { string temp = sr.ReadLine(); while (!string.IsNullOrEmpty(temp)) { returnValue += temp + "\\r\\n"; temp = sr.ReadLine(); } sr.Close(); } return returnValue;
}
-- modified at 17:44 Thursday 23rd August, 2007
-
C# is case sensative on variable names, so your use of s and S were a problem. Also having an IF statement to check for null inside your while statement that is calready checking for null makes no sense. But I guess you want to make sure that it's not null. Here's my cleanup of your code. I also ignored empty strings, but if you want to keep those, then change the while comparison to be while(temp != null)
private string Readlog(string path)
{
string returnValue = string.Empty;using (StreamReader sr = File.OpenText(path)) { string temp = sr.ReadLine(); while (!string.IsNullOrEmpty(temp)) { returnValue += temp + "\\r\\n"; temp = sr.ReadLine(); } sr.Close(); } return returnValue;
}
-- modified at 17:44 Thursday 23rd August, 2007
BLAH!!!! I cannot believe I totally missed the string concat inside a loop. Someone please pass me a gun now. FIXED version
private string Readlog(string path)
{
StringBuilder sb = new StringBuilder();using (StreamReader sr = File.OpenText(path)) { string temp = sr.ReadLine(); while (!string.IsNullOrEmpty(temp)) { sb.AppendLine(temp); temp = sr.ReadLine(); } sr.Close(); } return sb.ToString();
}
-
C# is case sensative on variable names, so your use of s and S were a problem. Also having an IF statement to check for null inside your while statement that is calready checking for null makes no sense. But I guess you want to make sure that it's not null. Here's my cleanup of your code. I also ignored empty strings, but if you want to keep those, then change the while comparison to be while(temp != null)
private string Readlog(string path)
{
string returnValue = string.Empty;using (StreamReader sr = File.OpenText(path)) { string temp = sr.ReadLine(); while (!string.IsNullOrEmpty(temp)) { returnValue += temp + "\\r\\n"; temp = sr.ReadLine(); } sr.Close(); } return returnValue;
}
-- modified at 17:44 Thursday 23rd August, 2007
Thanks for all your help I really appreciate the string.isnullorempty(temp) In are application it doesn't matter because we are joining together .csv files but if you were to use it for say a text file or something if there was a empty space it would stop at the dump out of the loop. again I really appreciate it thanks
-
Thanks for all your help I really appreciate the string.isnullorempty(temp) In are application it doesn't matter because we are joining together .csv files but if you were to use it for say a text file or something if there was a empty space it would stop at the dump out of the loop. again I really appreciate it thanks
Actually, it doesn't dump out of the loop if there's a space, it just doesn't append it to the output string. The while loop will continue to process until EOF is reached on the file. If you wanted it to stop the loop if an emptry string were to occur, you would need to add a break; statement in there. -- modified at 19:02 Thursday 23rd August, 2007 BTW, I am zerosity, just had to change the username for the forums as I never did that when I registered way back in time. :D