Help With Variable Scope!

G

grif

Hi everyone!

Been a few weeks since i've asked a noob question :)

At the moment I'm writing my First Form application compared to the few
console bits and pieces that ive been working on. And due to me reading a
few new books etc i've started cleaning up my code and sticking the engine
part into its own class and methods to tidy it up.

The thing i'm struggling with (due to my lack of understanding) is the scope
of variables. I understand that the block of code which they are declared in
are determines their scope etc but the trouble i'm having is carrying them
accross to the main form etc. So for example:

If I have the main form code with a click event say Form1.cs

Which has something like:

staticclass.Counter();
richtextbox1.Text = lineCount;

Basically calling the counter function from the class and the coutner
function counts the lines in a textfile etc where the variable lineCount is
declared....how do I carry that back to the main form so that I can insert
it into a textbox etc.

Obviously at the moment its giving me a not decalred scope error....i'm
guessing that It has to do with the arguments of the method in the
class....but what do i need to do to persist the variable etc???

Sorry for the newb questions...im still learning :)

Cheers
steve
 
G

Guest

Hi Steve,
From your description i guess that you have the variable "lineCount"
declared in the class "staticclass". In that case you have to specify the
context of the variable also like given below:
staticclass.Counter();
richtextbox1.Text = staticclass.lineCount.ToString();

but i think it will be a better option to make the Counter() method return
the value directly, so that you can say
"richtextbox1.Text=staticclass.Counter().ToString();"

Cheers,
Nimesh
 
G

grif

Yup, thats really what i'm trying to do is make the counter method return
the variable....so how do i do that???

This is the code snippets to date:

The calling statement from the main form:

staticclass.Counter();
txttotal.Text = Convert.ToString.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("url.txt");
String line;

while ((line = sr.ReadLine()) != null)
{
lineCount++
}
}

So as you can see im trying to call the method from the main form and then
checking how many lines of text etc and then trying to return it to a
variable. I tried defining the exact object and method like you suggested
but it still gave me the not in current context error etc.

Any ideas?

Steve
 
G

Guest

oops.. i think you missed to put the "return" statement. see the lines i
changed below.

hope this helps.

Cheers,
Nimesh

grif said:
Yup, thats really what i'm trying to do is make the counter method return
the variable....so how do i do that???

This is the code snippets to date:

The calling statement from the main form:

staticclass.Counter();
txttotal.Text = Convert.ToString.linecount;

//change this line to:
txttotal.Text = staticclass.Counter().ToString();
And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("url.txt");
String line;

while ((line = sr.ReadLine()) != null)
{
lineCount++
}

//Add line here
return lineCount;
 
J

Jon Skeet [C# MVP]

grif said:
Yup, thats really what i'm trying to do is make the counter method return
the variable....so how do i do that???

This is the code snippets to date:

The calling statement from the main form:

staticclass.Counter();
txttotal.Text = Convert.ToString.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("url.txt");
String line;

while ((line = sr.ReadLine()) != null)
{
lineCount++
}
}

So as you can see im trying to call the method from the main form and then
checking how many lines of text etc and then trying to return it to a
variable. I tried defining the exact object and method like you suggested
but it still gave me the not in current context error etc.

Any ideas?

You just need to end your Counter method with:

return lineCount;

That will return the value of the variable to the caller.
 
G

grif

Sorry guys, I missed typing that in when i chucked the code up here...I do
have the return lineCount; statement in place...outside the while loop so
that when its done it returns....but for some reason im still getting the
doesnt exist in this context error.

Steve
 
G

Guest

grif said:
Yup, thats really what i'm trying to do is make the counter method return
the variable....so how do i do that???

This is the code snippets to date:

The calling statement from the main form:

staticclass.Counter();
txttotal.Text = Convert.ToString.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("url.txt");
String line;

while ((line = sr.ReadLine()) != null)
{
lineCount++
}
}

So as you can see im trying to call the method from the main form and then
checking how many lines of text etc and then trying to return it to a
variable. I tried defining the exact object and method like you suggested
but it still gave me the not in current context error etc.

Any ideas?

Steve

The problem isn't that you can't reach the variable, the problem is that
it doesn't exist any more. The variable is a local variable in the
method, so it only exists while the method is running.

When Nimesh suggested that you could reach the variable by specifying
the class name, he assumed that the variable was a static variable in
the class so that the variable actually did exist after the method ended.

Just use

return lineCount;

as already has been suggested.
 
N

Nimesh

Steve,
If you are using the ffollowing lines to get the value, It won't work!
as you know, the staticclass.Counter(); function call returns the count. you
have to either store it to a local variable and then assign it to the
textbox or directly assign it to the text box.
ie, either of the following blocks will work:

int linecount = staticclass.Counter();
txttotal.Text = lineCount.ToString()

or
txttotal.Text =staticclass.Counter().ToString();

Cheers,
Nimesh


grif said:
Sorry guys, I missed typing that in when i chucked the code up here...I do
have the return lineCount; statement in place...outside the while loop so
that when its done it returns....but for some reason im still getting the
doesnt exist in this context error.

Steve
 
G

grif

Yup that worked beautifully :)

So I take it the returned value returns ONLY to the calling declaration???
how ould you handle it if the methor returns 2 or 3 variables????

Cheers
Steve
 
N

Nimesh

One function can return only one value.
You can use "ref" arguments to get more than value back from a function(but
it is not same as return).

Cheers,
Nimesh

grif said:
Yup that worked beautifully :)

So I take it the returned value returns ONLY to the calling declaration???
how ould you handle it if the methor returns 2 or 3 variables????

Cheers
Steve
 
J

Jon Skeet [C# MVP]

grif said:
Yup that worked beautifully :)

So I take it the returned value returns ONLY to the calling declaration???
how ould you handle it if the methor returns 2 or 3 variables????

Normally, it's best to make a method do one thing, and return a single
result. If you really *have* to return multiple results, pass the
parameters by reference using "ref" or "out". See
http://pobox.com/~skeet/csharp/parameters.html for more details on
this.
 
G

Guest

grif said:
Yup that worked beautifully :)

So I take it the returned value returns ONLY to the calling
declaration???
Yes.

how ould you handle it if the methor returns 2 or 3
variables????

There are several way. You can return an array, a list or a dictionary
if the values are of the same type. You can put the values in a custom
class or struct and return it. Or you can use the out or ref keywords to
make output arguments.
 

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