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

  • SEARCH
  • Home
  • 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 6000183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:39:26+00:00 2026-05-23T00:39:26+00:00

I have a struct like this: public struct MapTile { public int bgAnimation; public

  • 0

I have a struct like this:

public struct MapTile
{
    public int bgAnimation;
    public int bgFrame;
}

But when I loop over it with foreach to change animation frame I can’t do it…

Here’s the code:

foreach (KeyValuePair<string, MapTile> tile in tilesData)
{
        if (tilesData[tile.Key].bgFrame >= tilesData[tile.Key].bgAnimation)
        {
            tilesData[tile.Key].bgFrame = 0;
        }
        else
        {
            tilesData[tile.Key].bgFrame++;
        }
}

It gives me compile arror:

Error 1 Cannot modify the return value of 'System.Collections.Generic.Dictionary<string,Warudo.MapTile>.this[string]' because it is not a variable
Error 2 Cannot modify the return value of 'System.Collections.Generic.Dictionary<string,Warudo.MapTile>.this[string]' because it is not a variable

Why can’t I change a value inside a struct which is inside a dictionary?

  • 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-23T00:39:27+00:00Added an answer on May 23, 2026 at 12:39 am

    The indexer will return a copy of the value. Making a change to that copy won’t do anything to the value within the dictionary… the compiler is stopping you from writing buggy code. If you want to do modify the value in the dictionary, you’ll need to use something like:

    // Note: copying the contents to start with as you can't modify a collection
    // while iterating over it
    foreach (KeyValuePair<string, MapTile> pair in tilesData.ToList())
    {
        MapTile tile = pair.Value;
        tile.bgFrame = tile.bgFrame >= tile.bgAnimation ? 0 : tile.bgFrame + 1;
        tilesData[pair.Key] = tile;
    }
    

    Note that this is also avoiding doing multiple lookups for no good reason, which your original code was doing.

    Personally I’d strongly advise against having a mutable struct to start with, mind you…

    Of course, another alternative is to make it a reference type, at which point you could use:

    // If MapTile is a reference type...
    // No need to copy anything this time; we're not changing the value in the
    // dictionary, which is just a reference. Also, we don't care about the
    // key this time.
    foreach (MapTile tile in tilesData.Values)
    {
        tile.bgFrame = tile.bgFrame >= tile.bgAnimation ? 0 : tile.bgFrame + 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct like this: public struct stuff { public int ID; public
Suppose I have some simple struct like this: public struct WeightedInt { public int
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
Let's say I have a first structure like this: typedef struct { int ivalue;
Let's suppose I have a struct like this: struct my_struct { int a; int
I have a struct like this: public struct MyStruct { public string Name; public
I have a struct more or less like this: [Serializable] [XmlRoot(Customer)] public struct TCustomer
I have a C++ struct that looks like this: struct unmanagedstruct { int flags;
I have a datagrid which gets data like this: public struct MyData { public
Suppose I have a C# struct like this: [StructLayout(LayoutKind.Explicit)] struct IMAGE_DOS_HEADER { [FieldOffset(60)] public

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.