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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:11:21+00:00 2026-05-25T22:11:21+00:00

Sorry for this newbie question, I’m very new to C# and it’s semantics. I

  • 0

Sorry for this newbie question, I’m very new to C# and it’s semantics. I have the following struct:

public struct MT5Command{

    public MT5CommandType Type{ get;  set;}

    public AutoResetEvent ThreadWaitHandle { get; set; }

    public object InParam { get; set; }

    public object OutParam { get; set; }

}

And this code snippet:

MT5Command Cmd = new MT5Command();
Cmd.Type = MT5CommandType.GetServerInformation;
Cmd.ThreadWaitHandle = waitHandle.Value;
attributes = new ServerAttributes();
Cmd.OutParam = attributes;
....
ServerAttributes SrvAttributes = new ServerAttributes();
Cmd.OutParam = (ServerAttributes)SrvAttributes;

The last line does not compile: Cannot modify members of ‘command’ because it is a ‘foreach iteration variable’
How is it possible to assign the OutParam field to another ServerAttributes struct?

This is the outer for-each loop:

foreach (MT5Command Cmd in mCommandQueue.GetConsumingEnumerable())
{
   ...
}

Thanks, Juergen

  • 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-25T22:11:22+00:00Added an answer on May 25, 2026 at 10:11 pm

    You can’t, in this case. You haven’t given the full snippet, but I suspect it’s something like this:

    foreach (MT5Command command in someCollection)
    {
        // Code as posted
    }
    

    Now because MT5Command is a struct, you’re getting a copy of whatever value is in someCollection. Changing the property almost certainly wouldn’t do what you wanted it to anyway. The compiler is protecting you from yourself, by making the command variable read-only. You could do this:

    MT5Command copy = command;
    copy.OutParam = SrvAttributes;
    

    … but I strongly suspect that’s not what you want.

    As a rule, it’s a really bad idea to have mutable structs like the one you’ve come up with – and it doesn’t sound like that should be a struct in the first place.

    For more information:

    • My article on value types and reference types
    • MSDN guidelines on when it’s appropriate to create a struct
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry this is a very newbie question. We have this massive application I am
I'm really new to VSTO so sorry if this is a newbie question. I'm
I am new to SQLite and PHP so sorry about this newbie question. If
I'm pretty new to NSPredicate, and sorry if this is newbie question, but I'v
I got a newbie C++ question, sorry about this... In C++ if I have
Sorry, another python newbie question. I have a string: my_string = <p>this is some
perhaps it's a newbie question because I'm still new to java/android..sorry.. I'm getting this
I'm new to Objective-C so sorry if this is a newbie question. I've searched
I am sorry for the very newbie question, but this is driving me mad.
i'm new to django so i'm sorry for my newbie question i have a

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.