帮助变量范围! [英] Help With Variable Scope!

查看:51
本文介绍了帮助变量范围!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


几周后我问了一个菜鸟问题:)


目前我'我正在编写我的First Form应用程序,而不是我正在处理的几个
控制台的部分。由于我阅读了几本新书等等,我已经开始清理我的代码并坚持使用引擎

部分进入自己的类和方法来整理它。


我正在努力的事情(由于我缺乏理解)是范围

的变量。我知道他们在

中声明的代码块决定了他们的范围等但是我遇到的麻烦是带着它们

对主表单等例如:


如果我有一个带有点击事件的主表单代码,请说Form1.cs


其中有类似的内容:


staticclass.Counter();

richtextbox1.Text = lineCount;


基本上从中调用计数器功能class和coutner

函数计算文本文件中的行等,其中变量lineCount是

声明....如何将它带回主表单,以便我可以将

插入文本框等。


显然目前它给我一个不是十分范围的错误....我是

猜测它与

类中的方法的参数有关....但是我需要做些什么来保持变量等?


对不起newb qu我仍然在学习:)


干杯

史蒂夫

解决方案



你好史蒂夫,

从你的描述中我猜你有变量lineCount

在classstaticclass。在这种情况下,您必须指定变量的

上下文,如下所示:

staticclass.Counter();

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


但我认为这将是一个更好的选择,使Counter()方法直接返回值

,所以你可以说

" richtextbox1.Text = staticclass.Counter()。ToString();"


干杯,

Nimesh


" grif"写道:


大家好!


几周后我问了一个菜鸟问题:) />

目前我正在编写我的First Form应用程序,相比之下,我正在研究的几个

控制台的部分内容。由于我阅读了几本新书等等,我已经开始清理我的代码并坚持使用引擎

部分进入自己的类和方法来整理它。


我正在努力的事情(由于我缺乏理解)是范围

的变量。我知道他们在

中声明的代码块决定了他们的范围等但是我遇到的麻烦是带着它们

对主表单等例如:


如果我有一个带有点击事件的主表单代码,请说Form1.cs


其中有类似的内容:


staticclass.Counter();

richtextbox1.Text = lineCount;


基本上从中调用计数器功能class和coutner

函数计算文本文件中的行等,其中变量lineCount是

声明....如何将它带回主表单,以便我可以将

插入文本框等。


显然目前它给我一个不是十分范围的错误....我是

猜测它与

类中的方法的参数有关....但是我需要做些什么来保持变量等?


对不起newb问题s ...我还在学习:)


干杯

史蒂夫


是的,那就是我真正要做的就是让计数器方法返回

变量....所以我该怎么做?


这是迄今为止的代码片段:


来自主窗体的调用语句:


staticclass.Counter();

txttotal.Text = Convert.ToString.linecount;


现在班级部分:


公共类staticclass

{

public static int Counter()

{

int lineCount = 0;

StreamReader sr = new StremaReader(" url.txt");

String line;


while((line = sr.ReadLine() )!= null)

{

lineCount ++

}

}


所以你可以看到我试图从主窗体调用方法然后

检查多少行文本等然后尝试将它返回到

变量。我试着像你建议的那样定义确切的对象和方法

但是它仍然给了我当前上下文错误等等。


有什么想法吗?


史蒂夫


" Nimesh" < Ni **** @ discussion.microsoft.com写信息

新闻:DF ************************ ********** @ microsof t.com ...


>

Steve,
从您的描述中我猜您在类staticclass中声明了变量lineCount

。在这种情况下,您必须指定变量的

上下文,如下所示:

staticclass.Counter();

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


但我认为这将是一个更好的选择,使Counter()方法直接返回值

,所以你可以说

" richtextbox1.Text = staticclass.Counter()。ToString();"


干杯,

Nimesh


" grif"写道:


>大家好!

几周后我问了一个菜鸟问题:)

目前我正在编写我的First Form应用程序,而不是我正在研究的一些
控制台。由于我阅读了很少的新书等,我已经开始清理我的代码并将
引擎部分粘贴到自己的类和方法中来整理它。

我正在努力的事情(由于我缺乏理解)是变量的范围。我知道
中声明的代码块决定了它们的范围等,但我遇到的麻烦就是把它们带到了主要的表格等等。例如:

如果我有一个带有点击事件的主表单代码,请说Form1.cs

其中有类似的东西:

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

基本上从类中调用计数器函数和coutner
函数计算文本文件中的行等变量lineCount
是宣布....我如何将其带回主表格,以便我可以将其插入文本框等。

>显然目前它给了我一个不是十分范围的错误......我猜测它与
类中方法的参数有关....但是我需要做些什么来坚持变量等???

抱歉新手问题......我还在学习:)

干杯
史蒂夫


oops ..我想你错过了把返回声明。看到我在下面更改了
的行。


希望这会有所帮助。


干杯,

Nimesh


" grif"写道:


是的,这就是我真正想做的是让计数器方法返回

变量....那么我该怎么办呢?


这是迄今为止的代码片段:


来自主窗体的调用语句:


staticclass.Counter();

txttotal.Text = Convert.ToString.linecount;



//将此行更改为:

txttotal.Text = staticclass.Counter()。ToString();
< blockquote class =post_quotes>
>

现在班级部分:


公共类staticclass

{

public static int Counter()

{

int lineCount = 0;

StreamReader sr = new StremaReader (" url.txt");

字符串行;


while((line = sr.ReadLine())!= null)

{

lineCount ++

}



//在这里添加行

返回lineCount;


}


因此你可以看到我试图从主窗体调用方法然后

检查多少行文本等然后尝试将其返回到

变量。我试着像你建议的那样定义确切的对象和方法

但是它仍然给了我当前上下文错误等等。


有什么想法吗?


史蒂夫


" Nimesh" < Ni **** @ discussion.microsoft.com写信息

新闻:DF ************************ ********** @ microsof t.com ...



Steve,

来自您的描述我猜您在类staticclass中声明了变量lineCount

。在这种情况下,您必须指定变量的

上下文,如下所示:

staticclass.Counter();

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


但我认为这将是一个更好的选择,使Counter()方法直接返回值

,所以你可以说

" richtextbox1.Text = staticclass.Counter()。ToString();"


干杯,

Nimesh


" grif"写道:


大家好!


几周后我问了一个菜鸟问题:) />

目前我正在编写我的First Form应用程序,相比之下,我正在研究的几个

控制台的部分内容。由于我阅读了几本新书等等,我已经开始清理我的代码并将

引擎

部分放入其中自己的课程和方法来整理它。


我正在努力的事情(由于我缺乏理解)是

范围

变量。我知道他们被宣布为

的代码块确定了他们的范围等但是我遇到的麻烦是带着

他们

相当于主表格等所以例如:


如果我有主要表单代码和点击事件,请说Form1.cs


其中包含:


staticclass.Counter();

richtextbox1.Text = lineCount;


基本上从类和coutner调用计数器函数

函数计算文本文件中的行等变量lineCount



声明....我如何将它带回主表单,以便我可以将b
$ b插入文本框等等。 br />

显然目前它给了我一个不十分的范围错误....我是

猜测它与它的参数有关

类中的方法....但是我需要做些什么来保持变量等等?


很抱歉有新问题......我还在学习:)


干杯

史蒂夫


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

解决方案


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

"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.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


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

"Nimesh" <Ni****@discussions.microsoft.comwrote in message
news:DF**********************************@microsof t.com...

>
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

"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.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


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.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;

}

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****@discussions.microsoft.comwrote in message
news:DF**********************************@microsof t.com...


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

"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.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


这篇关于帮助变量范围!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆