班级成员的可见性/访问权限 [英] Visibility/Access of Class Members

查看:85
本文介绍了班级成员的可见性/访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这真的很简单,或者它应该......但是我被卡住了


在Windows窗体应用程序中,我有类似的东西:


Form1.cs

========


名称空间NS

Class Form1

{

公共标签label1


...


private void func1()

{

iterateRows();

}

}


Class1.cs

=========


使用NS;


公共类GetLabel

{

private void iterateRows()

{

Form1 .label1.Text =" some value";

}


}

方法iterateRows实际上是在执行循环操作我只想要

在每次迭代时在label1中显示值;简单吧?应该是

但是这里看不到label1 ......我在文档中找不到任何东西

这让我明白为什么Form1.label1.Text不可见。 ..这种类似的东西曾经是C中的一块蛋糕。 ..但是我已经远离编码

了一段时间...


我想我可以将对label1的引用传递给iterateRows()和使这个

工作?但是我不愿意,我想知道为什么这不起作用

无论如何


感谢您的帮助/解释......


L

解决方案

Liz,


如果我不错过我的猜测,真正的问题是Form1,而不是label1。由于

已编码,因此您可以看到label1,因为您将其声明为public。但是在Form1中它是




Form1是一个类名。它不是一个实例变量。换句话说

它是事物的蓝图,而不是事物本身。


在运行时,代码中的某个地方,你是创建

表格的*实例*,类似于:


表格myForm = new Form1();


myForm参考 - 或其副本 - 是你需要在你的GetLabel课程中引用




myForm.label1 .Text =some value;


问题是,myForm只是它所创建的代码的局部变量

并且在另一个班级中也看不到。这就是

封装的全部内容。


一些解决方案:


1)发送表格实例到GetLabel的构造函数:


表格myForm = new Form1();

GetLabel labelGetter = new GetLabel(myForm);


在GetLabel构造函数中,将该实例存储在

GetLabel的私有成员中,稍后您可以在GetLabel方法代码中引用它:


this.theForm.label1.Text =" Whatever";


2)GetLabel的公共财产接受表格:


GetLabel labelGetter = new GetLabel();

labelGetter.TheForm = new Form1();


3)另一种可能性是将表单实例存储在第三类,

表单管理器或全局对象缓存。这实际上是概念上与你可能想到的最接近的b $ b。例如:


public class Globals {

public static Form TheForm;

公共静态字符串ConnectionString;

//其他所需的东西

}


....

//无论你在哪里创建表格

Globals.TheForm = new Form1();

....

//在你的GetLabel课程中

Globals.TheForm.label1.Text =不管;


请注意,由于我们正在处理静态成员,我们可以通过它们来解决这些问题

没有Globals实例的类名。


所有上述代码都是袖手旁观,未经测试,不保证

适合特定用途的适销性或适用性,yadda-yadda,但

这可以让你朝着正确的方向前进。


两个最终和无关的建议词:


a)我不会将表单控件公开为公共字段。仅公开

必须可公开访问的内容,并使用描述性名称。对于

示例,编写一个名为,例如,RowLabelText的public * property *,并使其具有

getter和setter引用label1.Text。保持label1受到保护。


b)GetLabel描述的是一个动作,而不是一个东西,因此对于一个对象来说是一个糟糕的名字。

一个对象(这是一个东西)。 LabelGetter怎么样?


--BOB


Liz写道:

好的,这真的很简单,或者它应该......但是我被卡住了

在Windows窗体应用程序中,我有类似的东西:

Form1.cs
== ======

命名空间NS

Class Form1
{
公共标签label1

...

private void func1()
{
iterateRows();
}


Class1.cs
=========
使用NS;

公共类GetLabel
{
private void iterateRows()
{
Form1.label1.Text =有些价值;
}


方法iterateRows实际上是在执行循环操作而我只是希望
在每次迭代时在label1中显示值;简单吧?应该是
但是label1在这里是看不到的......我在文档中找不到任何内容
哪个让我明白为什么Form1.label1.Text不可见......这种
曾经是C中的一块蛋糕的东西。 ..但是我已经远离编码了一段时间......

我想我可以将对label1的引用传递给iterateRows()并使其工作吗?但我不愿意,我想知道为什么这不起作用

感谢您的帮助/解释......





Liz,


如果我不错过我的猜测,真正的问题是Form1,而不是label1。由于

已编码,因此您可以看到label1,因为您将其声明为public。但是在Form1中它是




Form1是一个类名。它不是一个实例变量。换句话说

它是事物的蓝图,而不是事物本身。


在运行时,代码中的某个地方,你是创建

表格的*实例*,类似于:


表格myForm = new Form1();


myForm参考 - 或其副本 - 是你需要在你的GetLabel课程中引用




myForm.label1 .Text =some value;


问题是,myForm只是它所创建的代码的局部变量

并且在另一个班级中也看不到。这就是

封装的全部内容。


一些解决方案:


1)发送表格实例到GetLabel的构造函数:


表格myForm = new Form1();

GetLabel labelGetter = new GetLabel(myForm);


在GetLabel构造函数中,将该实例存储在

GetLabel的私有成员中,稍后您可以在GetLabel方法代码中引用它:


this.theForm.label1.Text =" Whatever";


2)GetLabel的公共财产接受表格:


GetLabel labelGetter = new GetLabel();

labelGetter.TheForm = new Form1();


3)另一种可能性是将表单实例存储在第三类,

表单管理器或全局对象缓存。这实际上是概念上与你可能想到的最接近的b $ b。例如:


public class Globals {

public static Form TheForm;

公共静态字符串ConnectionString;

//其他所需的东西

}


....

//无论你在哪里创建表格

Globals.TheForm = new Form1();

....

//在你的GetLabel课程中

Globals.TheForm.label1.Text =不管;


请注意,由于我们正在处理静态成员,我们可以通过它们来解决这些问题

没有Globals实例的类名。


所有上述代码都是袖手旁观,未经测试,不保证

适合特定用途的适销性或适用性,yadda-yadda,但

这可以让你朝着正确的方向前进。


两个最终和无关的建议词:


a)我不会将表单控件公开为公共字段。仅公开

必须可公开访问的内容,并使用描述性名称。对于

示例,编写一个名为,例如,RowLabelText的public * property *,并使其具有

getter和setter引用label1.Text。保持label1受到保护。


b)GetLabel描述的是一个动作,而不是一个东西,因此对于一个对象来说是一个糟糕的名字。

一个对象(这是一个东西)。 LabelGetter怎么样?


--BOB


Liz写道:

好的,这真的很简单,或者它应该......但是我被卡住了

在Windows窗体应用程序中,我有类似的东西:

Form1.cs
== ======

命名空间NS

Class Form1
{
公共标签label1

...

private void func1()
{
iterateRows();
}


Class1.cs
=========
使用NS;

公共类GetLabel
{
private void iterateRows()
{
Form1.label1.Text =有些价值;
}


方法iterateRows实际上是在执行循环操作而我只是希望
在每次迭代时在label1中显示值;简单吧?应该是
但是label1在这里是看不到的......我在文档中找不到任何内容
哪个让我明白为什么Form1.label1.Text不可见......这种
曾经是C中的一块蛋糕的东西。 ..但是我已经远离编码了一段时间......

我想我可以将对label1的引用传递给iterateRows()并使其工作吗?但我不愿意,我想知道为什么这不起作用

感谢您的帮助/解释......







" Bob Grommes" < bo*@bobgrommes.com>在消息中写道

新闻:#b ************** @ TK2MSFTNGP09.phx.gbl ...

Liz,

如果我不错过我的猜测,真正的问题是Form1,而不是label1。由于
已编码,因此您可以看到label1,因为您将其声明为public。但它在Form1中是

Form1是一个类名。它不是一个实例变量。换句话说,它是事物的蓝图,而不是事物本身。


我得到的很多......但即使我这样做:


============ ====================================== ==========

公共类Form1

{

public Form myForm; //作为课堂声明


.....

}


static void Main(){

表单myForm = new Form1();

Application.Run(myForm)

}

=== =============================================== === =======


即使然后......在IDE中,如果我键入myForm。代码完成没有

显示我的公共var" label1" ......我不明白; IDE应该

理解 label1将是实例化的对象的成员var,不是吗?所以它应该让我为它做一个任务我会想的......但是它不会......(不,我不认为IDE坏了但我有

还没弄清楚我在这里做错了什么)


我只需将对label1的引用传递给
即可完成这项工作在另一个.CS文件中的
迭代方法..但是我不喜欢无法使用我的.CS中的参考label1做一个看似简单的事情。 $>
方法我打算用


其余的建议给我一些思考的东西......谢谢

输入请随意添加任何其他想法!


Liz


在运行时,代码中的某个地方,你是创建
表单的*实例*,类似于:

表单myForm = new Form1();

myForm参考 - 或副本它 - 你需要在你的GetLabel课程中引用


myForm.label1.Text =" some value" ;;

事实是,myForm只是它所创建的代码的局部变量,也不是在另一个班级中可见。这就是
封装的全部内容。

一些解决方案:

1)将表单实例发送到GetLabel的构造函数:

表单myForm = new Form1();
GetLabel labelGetter = new GetLabel(myForm);

在GetLabel构造函数中,将该实例存储在GetLabel的私有成员中,然后你可以在以后的GetLabel方法代码中引用它:

this.theForm.label1.Text =" Whatever" ;;

2)拥有GetLabel的公共财产接受表格:

GetLabel labelGetter = new GetLabel();
labelGetter.TheForm = new Form1();

3)另一种可能性是存储表单实例在第三类中,
表单管理器或全局对象缓存。实际上,这与你可能想到的最接近概念。例如:

public class Globals {
public static Form TheForm;
公共静态字符串ConnectionString;
//其他东西根据需要
}

//
//无论你在哪里创建表格
Globals.TheForm = new Form1();
...
//在你的GetLabel课程中
Globals.TheForm.label1.Text =" whatever"

请注意,既然我们正在处理静态成员,我们可以通过类名来解决它们必须有一个Globals的实例。

所有上述代码都是袖手旁观,未经测试,不保证
适销性或特定用途的适用性,yadda-yadda,但是
这应该会让你朝着正确的方向前进。

两个最终和无关的建议词:

a)我不会将表单控件公开为公共字段。仅公开
必须公开访问的内容,并使用描述性的名称。对于
示例,将一个名为public * property *的代码命名为RowLabelText,并使其具有
getter和setter引用label1.Text。保持label1受到保护。

b)GetLabel描述了一个动作,而不是一个东西,因此对象(这是一个东西)是一个糟糕的名称。 LabelGetter怎么样?

- BOB

Liz写道:

好吧,这真的很简单,或者应该是...但是我被困了

在Windows窗体应用程序中,我有类似的东西:

Form1.cs
========
Class Form1
{
公共标签label1

...

私有void func1()
{
iterateRows();
}


Class1.cs
======== =

使用NS;

公共类GetLabel
{void private iterateRows()
{
Form1.label1。 Text =some value;
}


方法iterateRows实际上是在执行循环操作而我只想
想显示值在每次迭代的label1中;简单吧?应该是
但是这里看不到label1 ...我在
文档中找不到任何东西,这清楚地告诉我为什么Form1.label1.Text不可见...这个
种类曾经是C中的一块蛋糕的东西。 ..但是我已经离开
编码了一段时间...

我想我可以将对label1的引用传递给iterateRows()并使
这个工作?但是我不愿意,而且我想明白为什么在任何情况下这都不会是b $ b工作

感谢您的帮助/解释......

L



ok, this is really simple stuff, or it should be ... but I''m stuck

In a Windows Forms app, I have something resembling this:

Form1.cs
========

namespace NS

Class Form1
{
public Label label1

...

private void func1()
{
iterateRows();
}
}

Class1.cs
=========

using NS;

public class GetLabel
{
private void iterateRows()
{
Form1.label1.Text = "some value";
}

}
the method iterateRows is really performing a loop operation and I just want
to show values in label1 with each iteration; simple, right ? should be
but label1 is not visible from here ... I cannot find anything in the docs
which makes clear to me why Form1.label1.Text is not visible ... this kind
of stuff used to be a piece of cake in "C" .. but I''ve been away from coding
for awhile ...

I suppose I could pass a reference to label1 to iterateRows() and make this
work ? but I''d rather not and I''d like to understand why this won''t work
in any case

Thanks for any help/explanations ...

L


解决方案

Liz,

If I don''t miss my guess, the real problem is Form1, not label1. As
coded, label1 would be visible, since you declared it public. But it''s
within Form1.

Form1 is a class name. It''s not an instance variable. In other words
it''s the blueprint for the thing, not the thing itself.

At runtime, somewhere in your code, you''re creating an *instance* of the
form, along the lines of:

Form myForm = new Form1();

The myForm reference -- or a copy of it -- is what you need to reference
in your GetLabel class:

myForm.label1.Text = "some value";

The thing is, myForm will just be a local variable to the code it''s
created by, and also not visible in another class. That''s what
encapsulation is all about.

Some solutions:

1) Send the form instance to the constructor of GetLabel:

Form myForm = new Form1();
GetLabel labelGetter = new GetLabel(myForm);

In the GetLabel constructor, store that instance in a private member of
GetLabel, then you can reference it later in GetLabel method code:

this.theForm.label1.Text = "Whatever";

2) Have a public property of GetLabel accept a Form:

GetLabel labelGetter = new GetLabel();
labelGetter.TheForm = new Form1();

3) Another possibility is to store the form instance in a third class,
either a form manager or a global objects cache. This is actually
closest conceptually to what you''re probably thinking of. For example:

public class Globals {
public static Form TheForm;
public static string ConnectionString;
// other stuff as needed
}

....
// wherever you create the form
Globals.TheForm = new Form1();
....
// within your GetLabel class
Globals.TheForm.label1.Text = "whatever";

Notice that since we''re dealing with static members, we can address them
through the class name without having to have an instance of Globals.

All of the above code is off-the-cuff and untested, no warranty as to
merchantability or fitness for a particular purpose, yadda-yadda, but
this should get you heading in the right direction.

Two final and unrelated words of advice:

a) I would not expose form controls as public fields. Expose only what
must be publicly accessible, and use a name that is descriptive. For
example, code a public *property* named, say, RowLabelText, and have its
getter and setter reference label1.Text. Keep label1 protected.

b) GetLabel describes an action, not a thing, and so is a lousy name for
an object (which is a thing). How about LabelGetter?

--Bob

Liz wrote:

ok, this is really simple stuff, or it should be ... but I''m stuck

In a Windows Forms app, I have something resembling this:

Form1.cs
========

namespace NS

Class Form1
{
public Label label1

...

private void func1()
{
iterateRows();
}
}

Class1.cs
=========

using NS;

public class GetLabel
{
private void iterateRows()
{
Form1.label1.Text = "some value";
}

}
the method iterateRows is really performing a loop operation and I just want
to show values in label1 with each iteration; simple, right ? should be
but label1 is not visible from here ... I cannot find anything in the docs
which makes clear to me why Form1.label1.Text is not visible ... this kind
of stuff used to be a piece of cake in "C" .. but I''ve been away from coding
for awhile ...

I suppose I could pass a reference to label1 to iterateRows() and make this
work ? but I''d rather not and I''d like to understand why this won''t work
in any case

Thanks for any help/explanations ...

L




Liz,

If I don''t miss my guess, the real problem is Form1, not label1. As
coded, label1 would be visible, since you declared it public. But it''s
within Form1.

Form1 is a class name. It''s not an instance variable. In other words
it''s the blueprint for the thing, not the thing itself.

At runtime, somewhere in your code, you''re creating an *instance* of the
form, along the lines of:

Form myForm = new Form1();

The myForm reference -- or a copy of it -- is what you need to reference
in your GetLabel class:

myForm.label1.Text = "some value";

The thing is, myForm will just be a local variable to the code it''s
created by, and also not visible in another class. That''s what
encapsulation is all about.

Some solutions:

1) Send the form instance to the constructor of GetLabel:

Form myForm = new Form1();
GetLabel labelGetter = new GetLabel(myForm);

In the GetLabel constructor, store that instance in a private member of
GetLabel, then you can reference it later in GetLabel method code:

this.theForm.label1.Text = "Whatever";

2) Have a public property of GetLabel accept a Form:

GetLabel labelGetter = new GetLabel();
labelGetter.TheForm = new Form1();

3) Another possibility is to store the form instance in a third class,
either a form manager or a global objects cache. This is actually
closest conceptually to what you''re probably thinking of. For example:

public class Globals {
public static Form TheForm;
public static string ConnectionString;
// other stuff as needed
}

....
// wherever you create the form
Globals.TheForm = new Form1();
....
// within your GetLabel class
Globals.TheForm.label1.Text = "whatever";

Notice that since we''re dealing with static members, we can address them
through the class name without having to have an instance of Globals.

All of the above code is off-the-cuff and untested, no warranty as to
merchantability or fitness for a particular purpose, yadda-yadda, but
this should get you heading in the right direction.

Two final and unrelated words of advice:

a) I would not expose form controls as public fields. Expose only what
must be publicly accessible, and use a name that is descriptive. For
example, code a public *property* named, say, RowLabelText, and have its
getter and setter reference label1.Text. Keep label1 protected.

b) GetLabel describes an action, not a thing, and so is a lousy name for
an object (which is a thing). How about LabelGetter?

--Bob

Liz wrote:

ok, this is really simple stuff, or it should be ... but I''m stuck

In a Windows Forms app, I have something resembling this:

Form1.cs
========

namespace NS

Class Form1
{
public Label label1

...

private void func1()
{
iterateRows();
}
}

Class1.cs
=========

using NS;

public class GetLabel
{
private void iterateRows()
{
Form1.label1.Text = "some value";
}

}
the method iterateRows is really performing a loop operation and I just want
to show values in label1 with each iteration; simple, right ? should be
but label1 is not visible from here ... I cannot find anything in the docs
which makes clear to me why Form1.label1.Text is not visible ... this kind
of stuff used to be a piece of cake in "C" .. but I''ve been away from coding
for awhile ...

I suppose I could pass a reference to label1 to iterateRows() and make this
work ? but I''d rather not and I''d like to understand why this won''t work
in any case

Thanks for any help/explanations ...

L





"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:#b**************@TK2MSFTNGP09.phx.gbl...

Liz,

If I don''t miss my guess, the real problem is Form1, not label1. As
coded, label1 would be visible, since you declared it public. But it''s
within Form1.

Form1 is a class name. It''s not an instance variable. In other words
it''s the blueprint for the thing, not the thing itself.
that much I get ... but even if I do this:

================================================== ==========
public class Form1
{
public Form myForm; // as a class declaration

.....
}

static void Main(){
Form myForm = new Form1();
Application.Run(myForm)
}
================================================== ==========

even then ... in the IDE, if I type "myForm." the code completion does not
show my public var "label1" ... this I don''t understand; the IDE should
"understand" that label1 WILL be a member var of the object when it''s
instantiated, no ? so it should let me make an assignment to it I would
think ... but it won''t ... (no, I don''t think the IDE is broken but I have
not yet figured out what I''m doing wrong here)

I CAN make this work simply by passing a reference to label1 to the
iteration method in the other .CS file .. but I don''t like not being able to
do a seemingly simple thing like reference label1 from my .CS with all the
methods I''m going to use

The rest of your suggestions give me some things to think about ... thanks
for the input and please feel free to add any other thoughts !

Liz


At runtime, somewhere in your code, you''re creating an *instance* of the
form, along the lines of:

Form myForm = new Form1();

The myForm reference -- or a copy of it -- is what you need to reference
in your GetLabel class:

myForm.label1.Text = "some value";

The thing is, myForm will just be a local variable to the code it''s
created by, and also not visible in another class. That''s what
encapsulation is all about.

Some solutions:

1) Send the form instance to the constructor of GetLabel:

Form myForm = new Form1();
GetLabel labelGetter = new GetLabel(myForm);

In the GetLabel constructor, store that instance in a private member of
GetLabel, then you can reference it later in GetLabel method code:

this.theForm.label1.Text = "Whatever";

2) Have a public property of GetLabel accept a Form:

GetLabel labelGetter = new GetLabel();
labelGetter.TheForm = new Form1();

3) Another possibility is to store the form instance in a third class,
either a form manager or a global objects cache. This is actually
closest conceptually to what you''re probably thinking of. For example:

public class Globals {
public static Form TheForm;
public static string ConnectionString;
// other stuff as needed
}

...
// wherever you create the form
Globals.TheForm = new Form1();
...
// within your GetLabel class
Globals.TheForm.label1.Text = "whatever";

Notice that since we''re dealing with static members, we can address them
through the class name without having to have an instance of Globals.

All of the above code is off-the-cuff and untested, no warranty as to
merchantability or fitness for a particular purpose, yadda-yadda, but
this should get you heading in the right direction.

Two final and unrelated words of advice:

a) I would not expose form controls as public fields. Expose only what
must be publicly accessible, and use a name that is descriptive. For
example, code a public *property* named, say, RowLabelText, and have its
getter and setter reference label1.Text. Keep label1 protected.

b) GetLabel describes an action, not a thing, and so is a lousy name for
an object (which is a thing). How about LabelGetter?

--Bob

Liz wrote:

ok, this is really simple stuff, or it should be ... but I''m stuck

In a Windows Forms app, I have something resembling this:

Form1.cs
========

namespace NS

Class Form1
{
public Label label1

...

private void func1()
{
iterateRows();
}
}

Class1.cs
=========

using NS;

public class GetLabel
{
private void iterateRows()
{
Form1.label1.Text = "some value";
}

}
the method iterateRows is really performing a loop operation and I just want to show values in label1 with each iteration; simple, right ? should be but label1 is not visible from here ... I cannot find anything in the docs which makes clear to me why Form1.label1.Text is not visible ... this kind of stuff used to be a piece of cake in "C" .. but I''ve been away from coding for awhile ...

I suppose I could pass a reference to label1 to iterateRows() and make this work ? but I''d rather not and I''d like to understand why this won''t work in any case

Thanks for any help/explanations ...

L



这篇关于班级成员的可见性/访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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