473,809 Members | 2,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With Variable Scope!

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.Cou nter();
richtextbox1.Te xt = 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

Sep 9 '07 #1
11 1604

Hi Steve,
From your description i guess that you have the variable "lineCount"
declared in the class "staticclas s". In that case you have to specify the
context of the variable also like given below:
staticclass.Cou nter();
richtextbox1.Te xt = staticclass.lin eCount.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.T ext=staticclass .Counter().ToSt ring();"

Cheers,
Nimesh

"grif" wrote:
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.Cou nter();
richtextbox1.Te xt = 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
Sep 9 '07 #2
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.Cou nter();
txttotal.Text = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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

"Nimesh" <Ni****@discuss ions.microsoft. comwrote in message
news:DF******** *************** ***********@mic rosoft.com...
>
Hi Steve,
From your description i guess that you have the variable "lineCount"
declared in the class "staticclas s". In that case you have to specify the
context of the variable also like given below:
staticclass.Cou nter();
richtextbox1.Te xt = staticclass.lin eCount.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.T ext=staticclass .Counter().ToSt ring();"

Cheers,
Nimesh

"grif" wrote:
>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.Co unter();
richtextbox1.T ext = 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....ho w 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
Sep 9 '07 #3
oops.. i think you missed to put the "return" statement. see the lines i
changed below.

hope this helps.

Cheers,
Nimesh

"grif" wrote:
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.Cou nter();
txttotal.Text = Convert.ToStrin g.linecount;
//change this line to:
txttotal.Text = staticclass.Cou nter().ToString ();
>
And now the class part:

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

while ((line = sr.ReadLine()) != null)
{
lineCount++
}
//Add line here
return 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

"Nimesh" <Ni****@discuss ions.microsoft. comwrote in message
news:DF******** *************** ***********@mic rosoft.com...

Hi Steve,
From your description i guess that you have the variable "lineCount"
declared in the class "staticclas s". In that case you have to specify the
context of the variable also like given below:
staticclass.Cou nter();
richtextbox1.Te xt = staticclass.lin eCount.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.T ext=staticclass .Counter().ToSt ring();"

Cheers,
Nimesh

"grif" wrote:
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.Cou nter();
richtextbox1.Te xt = 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
Sep 9 '07 #4
grif <al*********@ho tmail.comwrote:
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.Cou nter();
txttotal.Text = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 9 '07 #5
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

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
grif <al*********@ho tmail.comwrote:
>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.Co unter();
txttotal.Tex t = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 9 '07 #6
grif wrote:
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.Cou nter();
txttotal.Text = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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.

--
Göran Andersson
_____
http://www.guffa.com
Sep 9 '07 #7
Steve,
If you are using the ffollowing lines to get the value, It won't work!
>>staticclass.C ounter();
txttotal.Te xt = Convert.ToStrin g.linecount;
as you know, the staticclass.Cou nter(); 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.Cou nter();
txttotal.Text = lineCount.ToStr ing()

or
txttotal.Text =staticclass.Co unter().ToStrin g();

Cheers,
Nimesh
"grif" <al*********@ho tmail.comwrote in message
news:B5******** *************** ***********@mic rosoft.com...
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

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
>grif <al*********@ho tmail.comwrote:
>>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.C ounter();
txttotal.Te xt = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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.

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

Sep 9 '07 #8
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
"Göran Andersson" <gu***@guffa.co mwrote in message
news:O2******** ******@TK2MSFTN GP03.phx.gbl...
grif wrote:
>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.Co unter();
txttotal.Tex t = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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.

--
Göran Andersson
_____
http://www.guffa.com
Sep 9 '07 #9
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" <al*********@ho tmail.comwrote in message
news:BA******** *************** ***********@mic rosoft.com...
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
"Göran Andersson" <gu***@guffa.co mwrote in message
news:O2******** ******@TK2MSFTN GP03.phx.gbl...
>grif wrote:
>>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.C ounter();
txttotal.Te xt = Convert.ToStrin g.linecount;

And now the class part:

public class staticclass
{
public static int Counter()
{
int lineCount = 0;
StreamReader sr = new StremaReader("u rl.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.

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

Sep 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2018
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2 + el2; if (value1 > value2) return 1;
8
5484
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
4
14907
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
2
3026
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2 or more user controls dynamically by adding to a placeholder defined in page_load. I've included the sample code for how we are accessing one. The user controls are not rocket science - just a few text boxes with public accessor properties. We've...
23
19209
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
0
35252
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
2
4720
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
5
6139
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request scope. Application variables are then accessed in this manner Request.App.<Var>. To begin with I had a simple functioning login system inside a subdirectory named admin, this subdirectory had it's own application.cfm, I wasn't sure whether to duplicate...
112
5499
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.