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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:26:34+00:00 2026-05-25T16:26:34+00:00

folks, I’ve get compilation error(‘Can not modify the return value of dictionary because it’s

  • 0

folks,
I’ve get compilation error(‘Can not modify the return value of dictionary because it’s not a variable’) in next code :

public class BaseForm : Form
{

    protected void StoreGridViewPosition(DataGridView grid)
    {

        if (grids.ContainsKey(grid))
        {
            grids[grid].RowIndex = grid.CurrentCell.RowIndex;
            grids[grid].ColumnIndex = grid.CurrentCell.ColumnIndex;
        }

        Cell s = new Cell();
        s.RowIndex = 213;
    }

    protected void LoadGridViewPosition(DataGridView grid)
    {
    }

    private Dictionary<DataGridView, Cell> grids = new Dictionary<DataGridView, Cell>();

    private struct Cell
    {
        public int RowIndex;
        public int ColumnIndex;
    }
}

But if i’ll replace struct(Cell) with class, then it works fine.
Why it happens ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:26:35+00:00Added an answer on May 25, 2026 at 4:26 pm

    This won’t work as you expect. When you call:

    grids[grid].

    A copy of the struct is returned from the indexer, not a reference. So when you set into it:

    grids[grid].RowIndex = grid.CurrentCell.RowIndex;

    You are actually setting into a copy of the struct. This copy is then immediately discarded. All of this behaviour stems from the value type semantics of structs.

    All you can do if you use a struct is set a brand new struct into the cell:

    grids[grid] = new Cell { RowIndex = 3, ColumnIndex = 1 };

    Or pull a copy of the old one and set it back in (ignoring for a moment that structs should really always be made immutable 🙂 :

    var cell = grids[grid];
    cell.RowIndex = 3;
    grids[grid] = cell;
    

    Changing the definition to a class means the indexer returns a reference to that class, which you can mutate as both your reference and the dictionary’s reference are pointing at the same underlying object.

    The compiler is saying (in not so many words) that you are inadvertently trying to change a copy of what you think you are changing. You can make the same mistake easily if you expose a struct as a property of a class and try to mutate struct members thus:

    myClass.MyPointStruct.X = 2;

    (This appears to give the same error message in the new compiler at least, I could have sworn at one point it used to let you do this…)

    Or if you cast the struct to an interface, boxing boxes a copy.

    This question is very similar:

    Modify Struct variable in a Dictionary

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

Sidebar

Related Questions

folks, How can I differentiate between error codes and status codes in case of
Folks, we all know that IP blacklisting doesn't work - spammers can come in
Folks, why jQuery hover does not work? Here is the link - http://layot.prestatrend.com/ Thanks
Folks, I can't find the re-entrant version of syslog() for Linux...is there one? And
folks can you provide me the tutorial link or .pdf for learning basic COM?.
Folks, I'm using i nternal:merge tool since I'm not a big fan of GUI
Folks, It could be fairly easy but I could not find easy solution so
folks, Is it possible to solve ODE with complex variable in python? The equation
Folks, This is easy, but I cannot seem to get this right, any help
Folks, I am wondering if someone can explain to me what exactly is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.