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