file [?]

N

nicol. young20

Hi all;
I created a file & I want to put 2 number at that than use it in
another method
But it has 2 errors :
Error 1 Use of unassigned local variable 'a'
Error 2 Use of unassigned local variable 'b'

using System;
using System.IO;

class Program
{
static void Main(string[] args)
{
family();
}
static public void name()
{
TextWriter name1 = new StreamWriter("data1.txt");
int a = 3, b = 5;
name1.WriteLine(a);
name1.WriteLine(b);
name1.Close();
}
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();
}
}
 
K

Konrad Neitzel

Hi Nicol,

I created a file & I want to put 2 number at that than use it in
another method
But it has 2 errors :
Error 1 Use of unassigned local variable 'a'
Error 2 Use of unassigned local variable 'b'
static public void name()
{
TextWriter name1 = new StreamWriter("data1.txt");
int a = 3, b = 5;
name1.WriteLine(a);
name1.WriteLine(b);
name1.Close();
}
Ok, in this function you write some information to a file. That seems ok.
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();
}
}
You want to read the content of the the file and display it? But lets look
in detail, what your code is doing:
- You define 2 variables a and b.
- You create a TextReader to read data1.txt file
- You read the first line and display it
- Now you print the variable a - but you did nothing with that variable so
far (that is also said in the error description)
==> The content of the variable a is still on the next line of the file. So
you have to read it first. (e.g.:
if (Double.TryParse(name2.ReadLine, NumberStyles.Number,
CultureInfo.CurrentCulture, out a))
Console.WriteLine("a := {0}", a);
I gave all parameters to the parse function. At the moment I am not sure
what the default settings are. Parsing a number is normaly not that easy,
because it can be displayed in different ways:
- One thing that is important can be white spaces. You read a line and
normaly that should be fine how you wrote it. But if someone edits the file
manually and you get some leading spaces or so, it could make the Parse to
fail.
- A number can be displayed in multiple ways. a 1.000 can have different
meanings between cultures. It could mean the value 1 or a 1000! So I just
told the TryParse function to use the current culture in the hope that the
file will not be distributed between cultures.

But in your example, there is no need to parse the line. Our code is
parsing a string just to print it out again. But I I think you was
interested in getting the saved values again.

Another important point is:
There is a nice feature called serialisation. If you have an object, that
can be serialized, then you can easily write or read them to disk (or send
them over network or do everything, where serialisation is required!) Maybe
you want to have a closer look at that. It can takes a lot of pain from
you.

With kind regards,

Konrad
 
N

nicol

Hi Nicol,



Ok, in this function you write some information to a file. That seems ok.


You want to read the content of the the file and display it? But lets look
in detail, what your code is doing:
- You define 2 variables a and b.
- You create a TextReader to read data1.txt file
- You read the first line and display it
- Now you print the variable a - but you did nothing with that variable so
far (that is also said in the error description)
==> The content of the variable a is still on the next line of the file. So
you have to read it first. (e.g.:
            if (Double.TryParse(name2.ReadLine, NumberStyles.Number,
CultureInfo.CurrentCulture, out a))
                Console.WriteLine("a := {0}", a);
I gave all parameters to the parse function. At the moment I am not sure
what the default settings are. Parsing a number is normaly not that easy,
because it can be displayed in different ways:
- One thing that is important can be white spaces. You read a line and
normaly that should be fine how you wrote it. But if someone edits the file
manually and you get some leading spaces or so, it could make the Parse to
fail.
- A number can be displayed in multiple ways. a 1.000 can have different
meanings between cultures. It could mean the value 1 or a 1000! So I just
told the TryParse function to use the current culture in the hope that the
file will not be distributed between cultures.

But in  your example, there is no need to parse the line. Our code is
parsing a string just to print it out again. But I I think you was
interested in getting the saved values again.

Another important point is:
There is a nice feature called serialisation. If you have an object, that
can be serialized, then you can easily write or read them to disk (or send
them over network or do everything, where serialisation is required!) Maybe
you want to have a closer look at that. It  can takes a lot of pain from
you.

With kind regards,

Konrad

sorry kornard ;
i didn't get what u mean
 
P

Patrice

Hi,

You defined variables but you never gave them a value. So the compiler says
that you are using variables that were never assigned...

As you are doing nothing with a & b you could just suppress those lines and
your code will do the same thing...
 
N

nicol

Hi,


You defined variables but you never gave them a value. So the compiler says
that you are using variables that were never assigned...

As you are doing nothing with a & b you could just suppress those lines and
your code will do the same thing...

thanks Patric but i want to use a& b that i put them in file ;not a
new a & b;
 
K

kndg

thanks Patric but i want to use a& b that i put them in file ;not a
new a& b;

Hi Nicol,

static public void family()
{
int a, b;

TextReader name2 = new StreamReader("data1.txt");
Int32.TryParse(name2.ReadLine(), out a);
Int32.TryParse(name2.ReadLine(), out b);
name2.Close();

Console.WriteLine(a);
Console.WriteLine(b);
}

Make sure you understand what the 'out' paramater do. For this simple
senario, the above should suffice but for more complex senario, you
should look at the serialization/deserialization technique as Konrad had
suggested.

Regards.
 
K

Konrad Neitzel

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
 
N

nicol

Hi Nicol!


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 other2
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

thanks of all;
hi kornard i get what u mean ;thank u a l0t . . .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top