公共变量与使用属性不同? [英] Public variable not the same as using a property?

查看:65
本文介绍了公共变量与使用属性不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有公共变量的类,这与带有属性的

私有变量不一样吗?即,这不是:


Public Class MyTest

Public MyVar as String

End Class


与此相同:


公共类MyTest

私有_MyVar为字符串

属性MyVar < br $>
获取

返回_MyVar

结束获取

设置(byval value as String)

_MyVar =价值

结束套件

结束财产

结束班级

我一直都这么认为,事实上,我认为编译器将第一个转换为第二个。


然而,当我尝试将MyTest的通用列表数据绑定到gridview时

使用第一种语法,它失败了。当我使用第二种语法时,

成功。


有谁知道为什么?

解决方案
" DGK" < dg *@somewhere.comschrieb


如果我有一个带有公共变量的类,这不是一个

具有属性的私有变量?即,这不是:


Public Class MyTest

Public MyVar as String

End Class


与此相同:


公共类MyTest

私有_MyVar为字符串

属性MyVar < br $>
获取

返回_MyVar

结束获取

设置(byval value as String)

_MyVar =价值

结束套件

结束物业

结束等级


我一直都这么认为事实上,我认为编译器转换成第一个进入第二个。



不,它没有转换任何东西。第一个是公共字段,第二个是

属性。


然而,当我尝试将MyTest的通用列表数据绑定到a gridview

使用第一种语法,它失败了。当我使用第二种语法时,

成功。


有谁知道为什么?



可能是一个缺少的功能,它无法绑定到公共领域。

Armin


" DGK" < dg *@somewhere.com写信息

新闻:k8 ***************************** *** @ 4ax.com ...


如果我有一个带有公共变量的类,这不是一个

有属性的私有变量?即,这不是:


Public Class MyTest

Public MyVar as String

End Class


与此相同:


公共类MyTest

私有_MyVar为字符串

属性MyVar < br $>
获取

返回_MyVar

结束获取

设置(byval value as String)

_MyVar =价值

结束套件

结束物业

结束等级


我一直都这么认为事实上,我认为编译器将第一个转换为第二个。


然而,当我尝试将MyTest的通用列表数据绑定到gridview

使用第一种语法,它失败了。当我使用第二种语法时,

成功。


有谁知道为什么?



从设计角度来看,它们非常不同。恕我直言,你应该考虑差价。

如果你将变量声明为公共变量,你可以无限制地访问

变量到类之外的代码,从而将变量暴露给潜在的腐败并打破封装。通过声明变量

private,然后在您的类中提供公共属性,您可以获得

机会和控制对该变量的访问的能力

提供一个Set块,或者在Set块中添加逻辑

禁止不合逻辑的值。


在星期五,2007年9月14日08:55:28 -0500,PvdG42, < pv ** @ toadstool.edu>

写道:


>" dgk" < dg *@somewhere.com写信息
新闻:k8 ******************************** @ 4ax.com ..


>如果我有一个带有公共变量的类,这是否与具有属性的
私有变量相同?即,这不是:

Public Class MyTest
Public MyVar as String
End Class


同样如下:

Public Class MyTest
私有_MyVar as String
属性MyVar
获取
返回_MyVar
结束获取
设置(byval value as String)
_ MyVar =值
结束集
结束属性
结束类

我一直以为这样,其实我认为编译器转换了
第一个进入第二个。

然而,当我尝试使用第一个语法将MyTest的通用列表数据绑定到gridview时,它失败了。当我使用第二种语法时它会成功。

有谁知道为什么?


从设计角度来看,它们非常不同。恕我直言,你应该考虑区别。
如果你将变量声明为公共,你可以不受限制地访问该变量以便在类外部进行编码,从而将变量暴露给 private,然后在您的类中提供公共属性,您有机会和能力通过不提供Set块或通过添加来控制对该变量的访问Set块中的逻辑将禁止不合逻辑的值。



当然可以,但是如果你不在乎什么进出,那么它就是

a更清洁到只是声明一个公共变量。也可以更快地输入。

一行而不是六行或七行(即使使用片段)。


其中一件我不喜欢的事情关于VB是

属性的Readonly。我的意思是,如果没有Set,那么它就是readonly。我确定他们这是因为某种原因我做了b $ b,但是我不知道为什么。

好​​吧,无论如何,如果你打算使用Databind ,不要使用公共

属性。


If I have a class with a public variable, isn''t this the same as a
private variable with a property? ie, isn''t this:

Public Class MyTest
Public MyVar as String
End Class

the same as this:

Public Class MyTest
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
I always thought so, in fact, I thought that the complier converted
the first into the second.

Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.

Does anyone know why?

解决方案

"dgk" <dg*@somewhere.comschrieb

If I have a class with a public variable, isn''t this the same as a
private variable with a property? ie, isn''t this:

Public Class MyTest
Public MyVar as String
End Class

the same as this:

Public Class MyTest
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
I always thought so, in fact, I thought that the complier converted
the first into the second.

No, it doesn''t convert anything. The first is a public field, the second a
property.

Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.

Does anyone know why?

Probably a missing feature that it can not bind to public fields.
Armin


"dgk" <dg*@somewhere.comwrote in message
news:k8********************************@4ax.com...

If I have a class with a public variable, isn''t this the same as a
private variable with a property? ie, isn''t this:

Public Class MyTest
Public MyVar as String
End Class

the same as this:

Public Class MyTest
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
I always thought so, in fact, I thought that the complier converted
the first into the second.

Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.

Does anyone know why?

From a design perspective, they are very different. And IMHO, you should
consider the difference.
If you declare the variable as public, you are giving unrestricted access to
that variable to code outside the class, thus exposing the variable to
potential corruption and breaking encapsulation. By declaring the variable
private, then supplying a public property in your class, you have the
opportunity and ability to control access to that variable by either not
providing a Set block, or by adding logic in the Set block that will
disallow illogical values.


On Fri, 14 Sep 2007 08:55:28 -0500, "PvdG42" <pv**@toadstool.edu>
wrote:

>"dgk" <dg*@somewhere.comwrote in message
news:k8********************************@4ax.com.. .

>If I have a class with a public variable, isn''t this the same as a
private variable with a property? ie, isn''t this:

Public Class MyTest
Public MyVar as String
End Class

the same as this:

Public Class MyTest
Private _MyVar as String
Property MyVar
Get
Return _MyVar
End Get
Set (byval value as String)
_MyVar = value
End Set
End Property
End Class
I always thought so, in fact, I thought that the complier converted
the first into the second.

Yet, when I try to databind a generic list of MyTest to a gridview
using the first syntax, it fails. When I use the second syntax it
succeeds.

Does anyone know why?


From a design perspective, they are very different. And IMHO, you should
consider the difference.
If you declare the variable as public, you are giving unrestricted access to
that variable to code outside the class, thus exposing the variable to
potential corruption and breaking encapsulation. By declaring the variable
private, then supplying a public property in your class, you have the
opportunity and ability to control access to that variable by either not
providing a Set block, or by adding logic in the Set block that will
disallow illogical values.

Well sure, but if you don''t care what comes in or goes out, then it''s
a lot cleaner to just declare a public variable. Also quicker to type.
One line instead of six or seven (even if using a snippet).

One of the things I don''t like about VB is that Readonly on the
property. I mean, if there''s no Set, then it''s readonly. I''m sure they
did that for a reason but I can''t figure out why.
Well, anyway, if you''re planning on using Databind, don''t use public
properties.


这篇关于公共变量与使用属性不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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