0

I have a problem with my function

public class PS3
{
    public static void restoreAllClassesNames(string A, string B, string C/*, string A1, string B1, string C1, string A2, string B2, string C2, string A3, string B3, string C3, string A4, string B4, string C4*/)
    {
        A = returnLine("a.txt", 0);
        B = returnLine("a.txt", 1);
        C = returnLine("a.txt", 2);
    }

    public static string returnLine(string fileName, int line)
    {
        StreamReader SR = new StreamReader(fileName);
        List<string> myList = new List<string>();
        string linePath;
        while ((linePath = SR.ReadLine()) != null)
            myList.Add(linePath);

        return myList[line];
    }

So, when I am do this :

Functions.PS3.restoreAllClassesNames(textBox1.Text, textBox2.Text, textBox3.Text);

My textbox1 , 2 & 3 contains nothing, yet it should work

1
  • 3
    You might want to read this article about parameter passing. Commented Aug 21, 2014 at 22:21

3 Answers 3

6

You are passing in the value of the Text property of each TextBox, so changing that value within the restoreAllClassesNames method does nothing to the original control.

You can either pass in the controls themselves (since they are reference types):

public static void restoreAllClassesNames(Control A, Control B, Control C)
{
    A.Text = returnLine("a.txt", 0);
    B.Text = returnLine("a.txt", 1);
    C.Text = returnLine("a.txt", 2);
}

or make the strings out parameters:

public static void restoreAllClassesNames(out string A, out string B, out string C)
{
    A = returnLine("a.txt", 0);
    B = returnLine("a.txt", 1);
    C = returnLine("a.txt", 2);
}

and assign the text to the control from the calling method:

string a;
string b;
string c;

Functions.PS3.restoreAllClassesNames(out a, out b, out c);   

textBox1.Text = a;
textBox2.Text = b;
textBox3.Text = c;

you could also return a List<string>, a Tuple<string, string, string>, etc., etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, the code is leaking file handles because SR owns its underlying stream but isn't being closed afterwards.
0

StreamReader is looking for the file in bin\Debug\ folder

You can provide the filepath

public static string returnLine(string fileName, int line)
        {
            var filepath = "D:/" + fileName; /*Your file path*/
            if (File.Exists(filepath))
            {
                StreamReader SR = new StreamReader(filepath);
                List<string> myList = new List<string>();
                string linePath;
                while ((linePath = SR.ReadLine()) != null)
                    myList.Add(linePath);
                if (myList.Count > 0)
                    return myList[line];
                else
                    return "No record found";
            }
            else
            {
                return "File not found";
            }
        } 

Comments

-1

pass the reference of the string not its values :

  public static void restoreAllClassesNames( ref string A,  ref string B,  ref string C/*, string A1, string B1, string C1, string A2, string B2, string C2, string A3, string B3, string C3, string A4, string B4, string C4*/)
            {
                A = returnLine("a.txt", 0);
                B = returnLine("a.txt", 1);
                C = returnLine("a.txt", 2);
            }

You call your method like this

string txt1 = textBox1.Text;
string txt2 = textBox2.Text;
string txt3 = textBox3.Text;
    Functions.PS3.restoreAllClassesNames(ref txt1 , ref txt2 , ref txt3 );
textBox1.Text = txt1;
textBox2.Text = txt2;
textBox3.Text = txt3;

Check this link

4 Comments

You can't pass a property by reference.
No need to initialize txt1, txt2 and txt2; just make the parameters out instead of ref
@ThomasLevesque : You can use ref or out in this case ( twice can solve the problem) , there is no difference( for me) except the initialization
@LamloumiAfif, yes, but using ref is misleading for the caller, since it implies that the method will use the initial value of the parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.