473,804 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1604
>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.SequenceDia gramEditor.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.o rg> wrote in message
news:%2******** ********@TK2MSF TNGP12.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**********@n oway.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.co m>
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
6607
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
8
3997
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( Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This is the primary key of the table',
0
3945
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
6
1446
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 Property MyID() As SInteger Get
3
1816
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 webservice that returns a structure. On the client side I have added the webservice as a reference and created a class that contains the structure as a member as well as some other variables. The structure looks something like:
2
2211
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 i'm using the treenodes and each treenode needs a unique entry into my rich text box. After sitting at home with MSDN i've managed to get this functionality by storing a RTF string in the tag property of the treenode. On every 'before update' of the...
8
2063
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 as integer
8
2022
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: Public Class COM Private mMyProp As String Public Property MyProp() As String
5
2009
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 so. I have worked with dynamically generated DataTables with customized DataRows. However, these classes are usually pretty limited. For instance, you are limited to a select few data types and you can't deal with nulls as well as desired (in...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.