473,779 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't modify non-variable compiler bug

Hi,
C# compiler issues error message on the following lines:
GetStruct().Dep th = 10;
if GetStruct() returns structure (value type). It seems to be correct - else
you will modify temporary value on the stack.
It still allows to call method, so GetStruct().Set Depth(10) compiles
perfectly, although obviosly still modifies only temporary value on the
stack.
But in our case "Depth" is property, and its "set" implementation modifies
some external object - our structure is just wrapper around
reference to other object. We made this wrapper value type to prevent
excessive memory allocations for each GetStruct() call.
So in our case it makes sense to write GetStruct().Set Depth(10) - it
modifies non-temporary object,
and if you write on the next line Assert(GetStruc t().Depth == 10) -
assertion isn't failed.

It looks like compiler should threat property call here as method call, not
as simple field assignment.

Any ideas ?

Alexander Arlievsky
C.T.O. mPrest Ltd.
__***********@b la.mprest.spam. com.nospam
Nov 15 '05 #1
3 1152
Alexander,

I don't quite understand why you don't use a reference type for this.
In doing something like this:

// Set the value on the structure.
GetStruct().Dep th = 10;

You are actually allocating the memory on the stack for the structure.
Then, you are boxing the structure. The reason the structure is boxed (a
new object instance is created on the heap anyways) is because you are doing
a property access, and not a simple field access.

I would make your type that is returned a reference type, since you are
going to have to allocate memory on the heap regardless of whether or not
you use a value type or reference type.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alexander Arlievsky" <__*******@bla. mprest.spam.com .nospam> wrote in
message news:OV******** *****@TK2MSFTNG P11.phx.gbl...
Hi,
C# compiler issues error message on the following lines:
GetStruct().Dep th = 10;
if GetStruct() returns structure (value type). It seems to be correct - else you will modify temporary value on the stack.
It still allows to call method, so GetStruct().Set Depth(10) compiles
perfectly, although obviosly still modifies only temporary value on the
stack.
But in our case "Depth" is property, and its "set" implementation modifies
some external object - our structure is just wrapper around
reference to other object. We made this wrapper value type to prevent
excessive memory allocations for each GetStruct() call.
So in our case it makes sense to write GetStruct().Set Depth(10) - it
modifies non-temporary object,
and if you write on the next line Assert(GetStruc t().Depth == 10) -
assertion isn't failed.

It looks like compiler should threat property call here as method call, not as simple field assignment.

Any ideas ?

Alexander Arlievsky
C.T.O. mPrest Ltd.
__***********@b la.mprest.spam. com.nospam

Nov 15 '05 #2
100

Nicholas,
// Set the value on the structure.
GetStruct().Dep th = 10;

You are actually allocating the memory on the stack for the structure.
Then, you are boxing the structure. The reason the structure is boxed (a
new object instance is created on the heap anyways) is because you are doing a property access, and not a simple field access.


I don't see any reason for the structure to be boxed. Calling methods on
value types doesn't cause any boxing even (at least with c#) if the method
is virtual.
Alexander is right. This won't allocate anything on the heap.
The problem is (IMHO) that the value types are ment to be used to hold
values and not references. Keeping references as a members of value types is
error prone. Anyway, it's legal.
Hence reporting GetStruct().Dep th = 10 as an error is overkill. It should've
been reported as warning instead. Had the compiler reported those cases as
warnings we would've had some way to suppress that warnings as well. Like
*new* keyword when hiding inherited methods.

Anyway, to go arround this the following can be used:
WrapperStruct ws = GetStruct();
ws.Depth() = 10;
This won't use more memory we just gave name of the returned value. This
will make the compiler happy because if we want to we can use the new value
of the property.
Nov 15 '05 #3
On Thu, 16 Oct 2003 08:22:21 -0400, "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote:
Alexander,

I don't quite understand why you don't use a reference type for this.
In doing something like this:

// Set the value on the structure.
GetStruct().De pth = 10;

You are actually allocating the memory on the stack for the structure.
Then, you are boxing the structure. The reason the structure is boxed (a
new object instance is created on the heap anyways) is because you are doing
a property access, and not a simple field access.

I would make your type that is returned a reference type, since you are
going to have to allocate memory on the heap regardless of whether or not
you use a value type or reference type.


What do you mean by that? i.e. enclose the struct in a class proxy?
I fooled around with this a little, and could not figure a way to make
GetStruct() return a reference without resorting to unsafe and
pointers...is there some syntax to do that directly?

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4365
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
28
12606
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one level3 argument for each level2 argument, etc. Suppose I code it similar to this fragment: int main( int argc, char *argv ) {
13
2516
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
12
2369
by: Michael B Allen | last post by:
Is it legit to modify static data like the following code? #include <stdlib.h> #include <stdio.h> struct tbl { int i; char *s; };
4
3876
by: santa19992000 | last post by:
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?. It displays to enter IP address, if user wants to change, then he enters, otherwise he hits keyboard, which should prompt next one, how can I do in C program?. Thanks. enter IP address:
10
5273
by: wackyphill | last post by:
If I have the datasource of a datagrid set to a dataTable, can I add rows to that table in a non UI thread w/o causing problems? Or do I really need to invoke a method running on the UI thread? Note the datagrid is readonly. Records can be selected from it but not modified.
16
2666
by: Christian Blackburn | last post by:
Hi Gang, I would like to use SetMenuItemBitmaps or an equivalent in VB 2003. Can somebody point me in the right direction? Thanks in Advance, Christian
15
1135
by: RBCC | last post by:
How do I modify a record in a textfile in vb? Do I need 2 files? JOhn --- Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
5
1846
by: Joyti | last post by:
Hi, Need help in solving issue. I m having one function: int alllowReset(TRAN & tranP) { if( tranP->name == "PQRS" || ... ..
7
1976
by: Sin Jeong-hun | last post by:
I've using thread a lot in C#, but all of them were just single method with no return value or paramenters. It may sound bizarre but, can't it be a class? I'm writing an Windows application that processes inputs from a serial port. The serial port data is processed when DataReceived event occurs from the SerialPort instance. If the DataReceived event handler and the MainForm are in the same thread, and the user is calling a method in...
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
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,...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
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
5372
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
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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.