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. Problem with returning a string from a sub

Problem with returning a string from a sub

Scheduled Pinned Locked Moved C#
help
5 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.
  • C Offline
    C Offline
    crash893
    wrote on last edited by
    #1

    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 like private 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) or Foreach something in something { writelog(merge.txt,readlog(something.filename); } Thanks for pointing me in the right direction

    C 1 Reply Last reply
    0
    • C crash893

      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 like private 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) or Foreach something in something { writelog(merge.txt,readlog(something.filename); } Thanks for pointing me in the right direction

      C Offline
      C Offline
      ChrisKo 0
      wrote on last edited by
      #2

      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

      C C 2 Replies Last reply
      0
      • C ChrisKo 0

        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

        C Offline
        C Offline
        ChrisKo 0
        wrote on last edited by
        #3

        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();			
        

        }

        1 Reply Last reply
        0
        • C ChrisKo 0

          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

          C Offline
          C Offline
          crash893
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • C crash893

            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

            C Offline
            C Offline
            ChrisKo 0
            wrote on last edited by
            #5

            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

            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