By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

Using a structure as a property in a class gives error when accessing properties of structure

P: n/a
I have a Structure I have created and am using it as a Public Property of a class. Here is the property.

------------------------------------------------------

Dim _MyID As SInteger

Public Property MyID() As SInteger

Get

Return _MyID

End Get

Set(ByVal Value As SInteger)

_MyID = Value

End Set

End Property

Here is the structure

--------------------------------------------------------

Public Structure SInteger



Private mi_Value As Integer

Private mb_IsNull As Boolean



Public Property Value() As Integer

Get

Return mi_Value

End Get

Set(ByVal Value As Integer)

mi_Value = Value

End Set

End Property



Public Property IsNull() As Boolean

Get

Return mb_IsNull

End Get

Set(ByVal Value As Boolean)

mb_IsNull = Value

End Set

End Property



End Structure

So....

In the class when i want to set the value of the Value or isNull property of the structure ...

MyID.Value = 5

I get an error: Expression is a value and therefore cannot be the target of an assignment.

But if I use

_MyID.Value=5 it works

What's the deal here???



Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
>What's the deal here???

Since SInteger is a structure, the property will return a copy of the
_MyID value. If you were allowed to do this assignment, it would only
affect this temporary copy, and not the private _MyID variable. Since
that would be pretty useless and probably not what you want, the
compiler doesn't allow it.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2

P: n/a
The problem is that SInteger is a value type, so when you write

MyID.Value = 5

The MyID call is returning a copy of the _MyID variable (not the _MyID variable
itself) as a temporary value. Any assignment to this temporary value is
meaningless because it is never used again.

Think of it like this:

SInteger tempCopy = MyID;
tempCopy.Value = 5;

I think the compiler is just trying to prevent you from erroneously believing
that you would actually be changing the value of _MyID with this expression when
you really would not be. That error message is probably preventing a ton of bugs.

What you would need to do is something like this:

MyID = new SInteger(5);

which assigns a new SInteger value to the MyID property.

When you use _MyID you are dealing with the variable directly so there is no
temporary copy involved.

The error message you get from the C# compiler is:
"Cannot modify the return value of 'your property name' because it is not a
variable"

Hope this helps

--
Rodger

<http://www.SequenceDiagramEditor.com>
Sequence Diagram Editor - A quick and easy way to draw and edit sequence diagrams.

D Witherspoon wrote:
I have a Structure I have created and am using it as a Public Property
of a class. Here is the property.

------------------------------------------------------

Dim _MyID As SInteger

Public Property MyID() As SInteger

Get

Return _MyID

End Get

Set(ByVal Value As SInteger)

_MyID = Value

End Set

End Property

Here is the structure

--------------------------------------------------------

Public Structure SInteger

Private mi_Value As Integer

Private mb_IsNull As Boolean

Public Property Value() As Integer

Get

Return mi_Value

End Get

Set(ByVal Value As Integer)

mi_Value = Value

End Set

End Property

Public Property IsNull() As Boolean

Get

Return mb_IsNull

End Get

Set(ByVal Value As Boolean)

mb_IsNull = Value

End Set

End Property

End Structure

So....

In the class when i want to set the value of the Value or isNull
property of the structure ...

MyID.Value = 5

I get an error: Expression is a value and therefore cannot be the target
of an assignment.

But if I use

_MyID.Value=5 it works

What's the deal here???


Jul 21 '05 #3

P: n/a
Thanks,

Is there a way to access the property ByRef then?

Or would I have better results using a class?
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
What's the deal here???


Since SInteger is a structure, the property will return a copy of the
_MyID value. If you were allowed to do this assignment, it would only
affect this temporary copy, and not the private _MyID variable. Since
that would be pretty useless and probably not what you want, the
compiler doesn't allow it.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jul 21 '05 #4

P: n/a
D Witherspoon <dw**********@noway.org> wrote:
Is there a way to access the property ByRef then?

Or would I have better results using a class?


Yes, you'd have better results using a class. Creating your own value
type should be a very rare event. Creating your own *mutable* value
type should be even rarer.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.