Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Resolved the problem of the cannot modify the return value of because it is not a variable
Next
Answered
Julia Verdier
  • 13
Julia Verdier
Asked: May 18, 20222022-05-18T10:41:11+00:00 2022-05-18T10:41:11+00:00In: csharp

Resolved the problem of the cannot modify the return value of because it is not a variable

  • 13

. Advertisement .

..3..

. Advertisement .

..4..

I am new to csharp and searching the “cannot modify the return value of because it is not a variable” to understand it better. It seems it doesn’t work as expected when I used some suggestions before. Here is the command line I use:

public Point Origin { get; set; }
 
 Origin.X = 10; // fails with CS1612

The error I’m getting is below:

Error Message: Cannot modify the return value of 'expression' because
  it is not a variable
  
  An attempt was made to modify a value type that was the result of an
  intermediate expression. Because the value is not persisted, the value
  will be unchanged. 
  
  To resolve this error, store the result of the expression in an
  intermediate value, or use a reference type for the intermediate
  expression.

Please give me the solution to this issue.

cannot modify the return value
  • 2 2 Answers
  • 82 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-07T08:19:19+00:00Added an answer on June 7, 2022 at 8:19 am

    The cause:

    This error happens because of Point, it is a value type (struct). It means that when you try to access the Origin property, you are accessing to a copy value of the class’s value but not the actual value (class). If you set the X Property on it, you will set the property on the copied property and then remove it and keep the original value. The compiler will warn you, perhaps this is not what you intended.

    Solution: 

    You can easily solve this error by changing the X value as these following steps:

    Origin = new Point(10, Origin.Y);
    • 5
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Paul Girault
    2022-05-25T20:16:34+00:00Added an answer on May 25, 2022 at 8:16 pm


    Using a backing variable won’t help.

    The Point type can be described as a Value type.

    The entire Point value must be assigned to the Origin property.

    Origin = new Point(10, Origin.Y);

    Problem is, when you access the Origin property, what you get back by the get? A copy of the Point structure in your Origin properties auto-created fields. This means that if you modify the X field, the copy will not affect the underlying one. This is detected by the compiler and you get an error.

    Even if your backing variable was your own, your get would look something like this:

    get { return myOrigin; }

    You would still need to return a copy the Point structure, and you’d receive the same error.

    Hmm… after reading your question carefully, perhaps you mean to modify the back variable directly within your class?

    myOrigin.X = 10;

    Yes, that is what you would require.

    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.