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 6148415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:16:35+00:00 2026-05-23T19:16:35+00:00

When I build the following, I get the error Cannot modify members of ‘pd’

  • 0

When I build the following, I get the error Cannot modify members of 'pd' because it is a 'foreach iteration variable'.

foreach (var w in tt.Weeks)
  foreach (var d in w.Days)
    foreach (var pd in d.Periods)
      if (pd.Type == TimetablePeriod.Types.Registration) pd.ClassId = clsId;

I understand why this error occurs, but I’m trying to think of a neat way to rewrite the code. I could use nested for loops, but is there a nice LINQ solution?

(In case it’s not clear, I’m trying to set the ClassId of all periods in all days of all weeks to clsId, wherever the PeriodType is TimetablePeriod.Types.Registration.)

  • 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-23T19:16:35+00:00Added an answer on May 23, 2026 at 7:16 pm

    I assume you have a struct like this:

    public struct Period
    {
        public SomeType ClassId{get;set;}
    }
    

    This means that any iteration over a collection using either LINQ or foreach will work on copies of the values. So if you modify them you only modify a temporary copy which will then be discarded. The original struct will remain untouched.

    For this and many other reasons mutable structs should be avoided unless you have a really good reason to use them. (Search for “Mutable structs evil” and you’ll find a lot)

    In your case you can simply replace struct with class. You then get reference semantics.

    Or you can use a for loop, which gives you direct access. But it will still not compile with the naive implementation:

    for(int i=i;i<list.Count;i++)
      list[i].ClassId=something;
    

    Because the indexer on lists is a property, which copies its results. With arrays this would work since the indexer of an array returns a managed reference(or something similar). For lists you need to do:

    for(int i=i;i<list.Count;i++)
    {
      var copy=list[i];
      copy.ClassId=something;
      list[i]=copy;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I build the following code, I get the error above. //Controller.h #import <UIKit/UIKit.h>
When creating a new build in Team Foundation Server, I get the following error
Trying to get MGTwitterEngine app to build. Getting one pesky error. The following ERROR.
My project is named silence. I get the following error even when I build
When I try to build my solution, I get the following error: Visual Studio
Sometimes when i try to build/compile a downloaded source, i get following warning: ld:
When I try to build my project I get the following message in the
Problem Description: Occassionally when debugging, I get the following error. I'm using visual studio
Getting following build error: error X2230: the type 'ASA.Log.ServiceLogger.IASALog' is in assembly 'ASA.Log.ServiceLogger' that
during build with Visual Studio C++ I get this error: error C2027: use of

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.