Home > Cannot Modify > Cannot Modify The Return Value Of Because It Is Not A Variable C#

Cannot Modify The Return Value Of Because It Is Not A Variable C#

Contents

Monday, November 17, 2008 12:16 PM 0 Sign in to vote   myStruct.TestVal = 5; To assign such a variable we need to create an instance of myStruct. Why can't a property be used? -- ----------- Thanks, Steve Feb 7 '06 #1 Post Reply Share this Question 14 Replies P: n/a dotnetchic The property construct is fine. And if, finally you realize at the very end you can't change the last one, since it ISN'T const, and it should be, and you fix a major design issue. Not the answer you're looking for? Check This Out

Care to elaborate? This is because, the expression has multiple parts, hence its parsing requires a storage of intermediate result which we are not allowed to modify. What the compiler means is that we cannot modify it on the expression itself. Both code samples are perfectly legal, and so the question about why one is illegal is nonsensical.

Cannot Modify The Return Value Of Because It Is Not A Variable C#

If it had the internal features that checked such things, then it'd know. Things like overloading hiding certain type conversions, for example. Point newOrigin = new Point(10, 10); Origin = newOrigin; Hope I made sense there share|improve this answer edited Apr 25 '13 at 7:38 nawfal 29.4k27188233 answered Nov 17 '09 at 9:52

There's nothing available to the compiler to let it know that the method changes the contents of the struct. Secondly, the reason taking "const" out prevents it from compiling is that you can't initialize an array with a size that isn't known at compile time. When you say "myArray[123].X = 5", "myArray[123]" is an array element and an array element is a variable. Cannot Modify The Return Value Of Because It Is Not A Variable List Feb 7 '06 #10 P: n/a Jeffrey Tan[MSFT] Hi Steve, Thanks for your post.

It just doesn't have any methods which mutate it. Cannot Modify The Return Value Of Because It Is Not A Variable Struct I'll just say that you'd have to have very specialized code for const not to be useful. IIUC it's only one additional indirection while fetching the value. http://stackoverflow.com/questions/18126289/cannot-modify-the-return-value-because-it-is-not-a-variable The latter seems really icky to me. –supercat Apr 25 '13 at 15:38 @supercat oh yes, overlooked that!

Feb 7 '06 #9 P: n/a Steve Teeples This explanation has clarified for me my mistake. Cannot Modify The Return Value Of Transform Rotation Because It Is Not A Variable I guess there's good and bad that comes with that. Privacy statement Help us improve MSDN. Not the answer you're looking for?

Cannot Modify The Return Value Of Because It Is Not A Variable Struct

Unless you're going to be creating and destroying millions (and I do mean _millions_) of these things, you're better off making it a class, not a struct. click for more info With a List

It also prevents string folding, which increases the size of the binary, which has its own set of performance problems. his comment is here Using the List.get_Item you work on a return value, that is, a copy of the structure. And having "const" is a large one. Do lists return copies of their elements? Cannot Modify The Expression Because It Is Not A Variable Struct

But that opens a whole new can of worms, including what is essentially the same problem that the C++ "const" keyword had: until every single function in the call chain supports asked 5 years ago viewed 6039 times active 5 years ago Upcoming Events 2016 Community Moderator Election ends Nov 22 Linked 27 Modify Struct variable in a Dictionary Related 337Why are How can I remove an Online Account? this contact form It just doesn't have any methods which mutate it. -- Jon Skeet - http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too May 14 '07

Pete May 16 '07 #39 P: n/a Peter Duniho On Wed, 16 May 2007 09:23:09 -0700, Zytan The problem (well, "a problem" anyway) is that lots of methods areessentially "const" C# Struct Cannot Modify Because It Is Not A Variable What if the implementation changes? struct Data { public Data(){} private int someData; public int TheData { get { return someData; } set { someData = value; } } } class Sample { Data myData =

It's job isn't to enforce style or good practices.

You could have also used Reflection as "legal C# syntax" to mutate an immutable class. And of course, likewise the compiler can't make optimizations requiring constant values based on non-constant values. Should the linker be required to carry this flag around too, so that when you import a reference to a struct type not compiled with the current project, you still have C# Mutable Struct Why can't I change the element itself?

Remember that structs have _value semantics_. But, it's becoming obvious that the c# designers just want us to use reference types when we can, and everything in the langauge is based on this, and thus, it is Mutable structs are strange beasts and tricky to work with. navigate here When you say "bar.X = 5", "bar" is a local variable.

what you're saying is, "Return me a copy of the TimeDelayData from the GlobalData object, then make a change to that copy, then throw it away." Throw it away because you B-Line Medical is seeking .NET Developers for exciting positions in medical product development in MD/DC. Try removing the constructor and see if it works. No, it wouldn't - because that's not how value types work.

Can clients learn their time zone on a network configured using RA? Even if you used your own backing variable your get would look like:- get { return myOrigin; } You'd still be returning a copy of the Point structure and you'd get I never had troubles with Lists in C++ like this. When you change like the above, myStruct instance inside the myClass remains intact.