Reputation: 1
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
Upvotes: 0
Views: 89
Reputation: 84
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";
}
}
Upvotes: 0
Reputation: 9081
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
Upvotes: -1
Reputation: 152566
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.
Upvotes: 6