物业的财产 [英] Property on a property

查看:89
本文介绍了物业的财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...为什么我不能进入物业的房产?


例如。


class Foo {

public SizeF MySize

{get {return ms; }

set {ms = value; }现在,如果我尝试:

Foo.MySize.Width = 5;


我得到:无法修改''Foo.MySize''的返回值,因为它不是

变量


为什么会这样?我正在创建一个Shape类的结构,其中基本单元

被用来拥有公共属性,例如它的位置和大小等。


我认为我有为了使公共属性不是最优的,因为我可能想要对set访问器进行一些验证。当然我可以使用我的

自己的get / set方法,但是当C#(。net)有属性时这有点错误。


reagards

Anders

解决方案

Flare


你必须将属性MySize声明为静态,要么实例化一些

Foo对象并使用它来获取SizeF属性。

在您的示例中,您指定静态样式访问同时使用非静态

property。


HTH

Alex


" Flare" <无**** @ sorry.dk>在留言中写道

新闻:ek ************* @ TK2MSFTNGP11.phx.gbl ...

嗨...为什么可以我访问过一处房产吗?

例如。

类Foo {
public SizeF MySize
{get {return ms; }
设置{ms = value;现在,如果我尝试:
Foo.MySize.Width = 5;

我得到:无法修改返回值''Foo.MySize''因为它不是变量

为什么会这样?我正在创建一个Shape类的结构,其中基本单元
被用来具有公共属性,例如它的位置和大小等。

我认为我必须制作公共属性而不是最佳因为我可能想对集合访问器进行一些验证。当然我可以使用
我自己的get / set方法,但是当C#(。net)有属性时这有点错误。


Anders


>我得到:无法修改''Foo.MySize''的返回值,因为它不是

变量

为什么会这样?我正在创建一个Shape类的结构,其中基本单元
被用来拥有公共属性,例如它的位置和大小等。




SizeF是一个值类型,所以如果你确实修改了返回的值,你将不会修改属性的内容,只返回返回的值(然后将丢弃
)。编译器正在帮助你。


对于这个特殊问题,请提供Width和Height属性以及

''MySize''属性。


n!


一个getter没有返回左值:即使访问
$ b $的语法b属性与字段的属性相同,属性未被分类为

变量。因此,无法将属性作为ref或out

参数传递。 - 10.6


嗯,如果周二这个getter返回一个左值,那就不好了,即

变量x本身...


public bool isTuesday;

public int x = 1;

public int X {

get

{

if(this.isTuesday)

返回this.x;

else

返回0;

}

//无设定者

}


所以客户代码可以说:


if(myFoo.IsTuesday)

myFoo.X = 9; //没有必要的setter,我们有变量!


;-)


Hi...Why can′t I access a property thorug property?

Eg.

class Foo {
public SizeF MySize
{ get { return ms; }
set { ms = value; }
}}

Now, if i try to:
Foo.MySize.Width = 5;

I get: Cannot modify the return value of ''Foo.MySize'' because it is not a
variable

Why is it so? Im creating a structure of Shape classes where the base unit
was supossed to have public properties on eg its location and size etc.

As i see i have to make public attributes instead wich not optimal since i
might want to do some validation on set accessors. Of course I could make my
own get/set methods but thats kind of wrong when C#(.net) has properties.

reagards
Anders

解决方案

Hi, Flare

you must either declare property MySize as static, either instantiate some
Foo object and use it to get to SizeF property.
In your sample you are specifying static-style access while using non-static
property.

HTH
Alex

"Flare" <no****@sorry.dk> wrote in message
news:ek*************@TK2MSFTNGP11.phx.gbl...

Hi...Why can′t I access a property thorug property?

Eg.

class Foo {
public SizeF MySize
{ get { return ms; }
set { ms = value; }
}}

Now, if i try to:
Foo.MySize.Width = 5;

I get: Cannot modify the return value of ''Foo.MySize'' because it is not a
variable

Why is it so? Im creating a structure of Shape classes where the base unit
was supossed to have public properties on eg its location and size etc.

As i see i have to make public attributes instead wich not optimal since i
might want to do some validation on set accessors. Of course I could make my own get/set methods but thats kind of wrong when C#(.net) has properties.

reagards
Anders



> I get: Cannot modify the return value of ''Foo.MySize'' because it is not a

variable

Why is it so? Im creating a structure of Shape classes where the base unit
was supossed to have public properties on eg its location and size etc.



SizeF is a value type, so if you did modify the returned value you wouldn''t
be modifying the contents of the property, just the returned value (which
would then be discarded). The compiler is helping you.

For this particular problem, provide Width and Height properties along with
the ''MySize'' property.

n!


A getter doesn''t return an lvalue: "Even though the syntax for accessing a
property is the same as that for a field, a property is not classified as a
variable. Thus, it is not possible to pass a property as a ref or out
argument." - 10.6

Hmm, wouldn''t it be nice if on Tuesday this getter returned an lvalue, i.e.,
the variable x itself...

public bool isTuesday;
public int x = 1;
public int X {
get
{
if (this.isTuesday)
return this.x;
else
return 0;
}
// no setter
}

so client code could say:

if (myFoo.IsTuesday)
myFoo.X = 9; // no setter necessary, we''ve got the variable!

;-)


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

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