473,672 Members | 2,577 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 1149
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
4361
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
12571
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
2507
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
2351
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
3870
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
5262
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
2656
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
1121
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
1840
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
1973
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
8486
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
8404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8828
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
8608
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
6238
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
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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.