473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
4 1588
>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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: D Witherspoon | last post by:
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...
3
by: Ren | last post by:
Hi all, I'm still rather new to .NET so I hope you'll bear with me as I try and explain my question. I am writing an ASP.NET application using VB.NET. I am accessing a web method from a...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
8
by: AMDRIT | last post by:
Hello everyone, How do I access a property from another thread in VB'05? Thanks public class frmblah inherits system.windows.forms.form private mCount as integer private property Count...
8
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class:...
5
by: jehugaleahsa | last post by:
Hello: I am trying to find what is the very best approach to business objects in Windows Forms. Windows Forms presents an awesome opportunity to use DataTables and I would like to continue doing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.