使用属性引用Struct数据 [英] Using property to reference Struct data

查看:66
本文介绍了使用属性引用Struct数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我不能使用属性来修改

结构中的数据。有人能告诉我为什么我会收到错误无法修改返回

的值myData.TheData"因为它不是一个变量。


这是休息时间。


struct Data

{

公共数据(){}

private int someData;

public int TheData

{

get {return someData; }

设置{someData = value; }

}

}


class Sample

{

数据myData = new Data();


void AMethod()

{

myData.TheData = 7;

}

}


如果我不使用该属性TheData而改为int someData public

我可以引用myData.someData = 7,它会正常工作。为什么我们不能使用
财产?


-

-----------

谢谢,

史蒂夫

I don''t understand why I cannot use a property to modify data within a
struct. Can someone tell me why I get the error "Cannot modify the return
value of "myData.TheData" because it is not a variable.

Here is what breaks.

struct Data
{
public Data(){}
private int someData;
public int TheData
{
get { return someData; }
set { someData = value; }
}
}

class Sample
{
Data myData = new Data();

void AMethod()
{
myData.TheData = 7;
}
}

If I don''t use the property "TheData" and instead make "int someData" public
I can reference myData.someData = 7 and it will work just fine. Why can''t a
property be used?

--
-----------
Thanks,
Steve

推荐答案

物业建筑很好。


我想知道你为什么声明一个结构的默认构造函数,

,因为C#不允许它们。尝试删除构造函数和

看看它是否有效。

The property construct is fine.

I''m wondering why you declared a default constructor for a struct,
though, since C# doesn''t allow them. Try removing the constructor and
see if it works.


这不是给你警告的代码。此代码无法使用错误编译
,Structs不能无参数

构造函数。请发布给您错误的确切代码。

This isn''t the code that gave you that warning. This code fails to
compile with the error, "Structs cannot have parameterless
constructor." Please post the exact code that gave you the error.


添加构造函数是我的错误。我在实际代码中没有它。


MS帮助评论说:


错误消息

无法修改''expression''的返回值,因为它不是变量


尝试修改由于
而导致的值类型
中间表达式。由于该值未持久化,因此值将保持不变。


要解决此错误,请将表达式的结果存储在中间

值,或者使用中间表达式的引用类型。

因此我创建了一个临时值并为其分配了我的结构。唯一的

就是在更新临时引用时它并没有更新我的结构。


数据tmp = myData;

tmp.TheData = 7;


这在tmp值中为7分配了7,但它没有更新myData

" data" ;。我无法理解。


myData是静态全局结构的一部分。静态性质可以和它有什么关系吗?

-

-----------

谢谢,

史蒂夫

" dotnetchic"写道:
Adding the constructor was my mistake. I don''t have it in the actual code.

A MS help comment says:

Error Message
Cannot modify the return value of ''expression'' because it is not a variable

An attempt was made to modify a value type that was the result of an
intermediate expression. Because the value is not persisted, the value will
be unchanged.

To resolve this error, store the result of the expression in an intermediate
value, or use a reference type for the intermediate expression.
I therefore created a temp value and assigned my structure to it. The only
thing is that when updating the temp reference it didn''t update my structure.

Data tmp = myData;
tmp.TheData = 7;

This assigned 7 to TheData in the tmp value, but it didn''t update the myData
"data". I can''t figure out way.

myData is part of a static global structure. Could the static nature have
anything to do with it?
--
-----------
Thanks,
Steve
"dotnetchic" wrote:
属性构造很好。

我想知道你为什么声明一个结构的默认构造函数,
虽然,因为C#不允许他们。尝试删除构造函数并查看它是否有效。
The property construct is fine.

I''m wondering why you declared a default constructor for a struct,
though, since C# doesn''t allow them. Try removing the constructor and
see if it works.



这篇关于使用属性引用Struct数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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