Results 1 to 3 of 3

Thread: [RESOLVED] Need help returning a string

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] Need help returning a string

    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

    c# Code:
    1. private void Readlog(string path)
    2.         {
    3.             StreamReader SR;
    4.             string S;
    5.             SR = File.OpenText(path);
    6.             S = SR.ReadLine();
    7.             while (S != null)
    8.             {
    9.                 if (S != null)
    10.                 {
    11.                    //this is sloppy but i cant have a trailing /r/n so this is the only                
    12.                     //way i can think to get rid of it                    
    13.                    S = S + "\r\n";
    14.                 }
    15.                 textBox1.AppendText(S);
    16.                 S = SR.ReadLine();
    17.             }
    18.             SR.Close();
    19.         }

    what i want to do is change it to something like

    c# Code:
    1. private string Readlog(string path)
    2.         {
    3.             string temp;
    4.             StreamReader SR;
    5.             string S ;
    6.             SR = File.OpenText(path);
    7.             S = SR.ReadLine();
    8.             temp =s
    9.             while (S != null)
    10.             {
    11.                 if (S != null)
    12.                 {
    13.                     S = S + "\r\n";
    14.                 }
    15.                 temp = temp +S;
    16.                 S = SR.ReadLine();
    17.             }
    18.             SR.Close();
    19.             return temp;
    20.         }



    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help returning a string

    Such a method already exists:
    C# Code:
    1. string fileContents = System.IO.File.ReadAllText(path);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Need help returning a string

    you got to be kidding me

    well if anyone wants a wheel i just reinvented one


    thanks jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width