Hi Nicol!
thanks Patric but i want to use a& b that i put them in file ;not a
new a & b;
I am really confused now and I am not sure, what information is missing on
your side. But maybe it was simply me, because I got confused with the
types. I was using double instead of your int types. Sorry for that. There
was some other posting about double parsing that confused me.
I will try to explain it a little bit further:
You made a function like this:
static public void name()
{
TextWriter name1 = new StreamWriter("data1.txt");
int a = 3, b = 5;
name1.WriteLine(a);
name1.WriteLine(b);
name1.Close();
}
The variables a and b are called local variables. They only exist in the
block of code, where you defined them (in this case, it is the function
name()).
So you cannot access these variables in any other function.
But you wrote the content of these local variables to a file called
data1.txt, so you can read the values again from that file.
(Which I thought that you want to do that!)
So we get to your next function:
static public void family()
{
int a, b;
TextReader name2 = new StreamReader("data1.txt");
Console.WriteLine(name2.ReadLine());
Console.WriteLine(a); // **********error
Console.WriteLine(b); //***********error
name2.Close();
}
Again you have 2 variables called a and b, but they have nothing to do with
the other 2 variables in the other function! Maybe we simply think of street
names. There are streets "a" and "b" in a city called "name()" and now you
have another city called "family()" and you have again 2 streets with the
names "a" and "b". But these streets have nothing in common except their
names!
But you saved some values into a file and you could read that file. We start
simply and read the file line by line:
static public void ReadFile()
{
TextReader reader = new StreamReader("data1.txt");
Console.WriteLine(reader.ReadLine());
Console.WriteLine(reader.ReadLine());
}
So when you call this function, you see the 2 values that was written in
your other function (if you called the other function so the file data1.txt
exists!).
The next step could be: you want to get the 2 lines of text (That is, what
you read from the file!) into some variables. But the variables are of type
int. So you have to parse the Text (Type: String) to a Int.
So what you can do is:
static public void ReadFile()
{
TextReader reader = new StreamReader("data1.txt");
int newA = Int32.Parse(reader.ReadLine());
int newB = Int32.Parse(reader.ReadLine());
// Now we have the values you stored to the file.
Console.WriteLine(newA);
Console.WriteLine(newB);
}
int is just a shortcut for the System.Int32 class and this class has a
Parse() function. This parse function takes a string and gives you back an
int.
I named the variables to newA and newB to just mak sure that you understand,
that these are new local variables that have nothing to do with the other 2
local variable in the other function.
And maybe you should read a little more about the Parse and TryParse
function and play around a little. They offer a lot more and can make your
code much better. But I will not go into much more detail here to avoid
confusing you (Sorry, if I simply brought to much in my answer.)
With kind regards,
Konrad