Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Help With Variable Scope!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
grif - 09 Sep 2007 10:42 GMT
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
Nimesh - 09 Sep 2007 10:58 GMT
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

> Hi everyone!
>
[quoted text clipped - 30 lines]
> Cheers
> steve
grif - 09 Sep 2007 12:08 GMT
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

> Hi Steve,
> From your description i guess that you have the variable "lineCount"
[quoted text clipped - 50 lines]
>> Cheers
>> steve
Nimesh - 09 Sep 2007 12:20 GMT
oops.. i think you missed to put  the "return" statement. see the lines i
changed below.

hope this helps.

Cheers,
Nimesh

> Yup, thats really what i'm trying to do is make the counter method return
> the variable....so how do i do that???
[quoted text clipped - 5 lines]
> staticclass.Counter();
> txttotal.Text = Convert.ToString.linecount;

//change this line to:
txttotal.Text = staticclass.Counter().ToString();

> And now the class part:
>
[quoted text clipped - 10 lines]
>                         lineCount++
>                  }

//Add  line here
return lineCount;

> }
>
[quoted text clipped - 61 lines]
> >> Cheers
> >> steve
Jon Skeet [C# MVP] - 09 Sep 2007 12:20 GMT
> Yup, thats really what i'm trying to do is make the counter method return
> the variable....so how do i do that???
[quoted text clipped - 28 lines]
>
> Any ideas?

You just need to end your Counter method with:

return lineCount;

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

grif - 09 Sep 2007 12:33 GMT
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

>> Yup, thats really what i'm trying to do is make the counter method return
>> the variable....so how do i do that???
[quoted text clipped - 35 lines]
>
> That will return the value of the variable to the caller.
Nimesh - 09 Sep 2007 12:53 GMT
Steve,
If you are using the ffollowing lines to get the value, It won't work!
>>> staticclass.Counter();
>>> txttotal.Text = Convert.ToString.linecount;

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

> 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
[quoted text clipped - 44 lines]
>>
>> That will return the value of the variable to the caller.
Göran Andersson - 09 Sep 2007 12:39 GMT
> Yup, thats really what i'm trying to do is make the counter method return
> the variable....so how do i do that???
[quoted text clipped - 30 lines]
>
> 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.

Signature

Göran Andersson
_____
http://www.guffa.com

grif - 09 Sep 2007 13:09 GMT
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
>> Yup, thats really what i'm trying to do is make the counter method return
>> the variable....so how do i do that???
[quoted text clipped - 45 lines]
>
> as already has been suggested.
Nimesh - 09 Sep 2007 13:19 GMT
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

> Yup that worked beautifully :)
>
[quoted text clipped - 54 lines]
>>
>> as already has been suggested.
Jon Skeet [C# MVP] - 09 Sep 2007 14:03 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Göran Andersson - 09 Sep 2007 19:24 GMT
> 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.

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.