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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:46:51+00:00 2026-05-15T20:46:51+00:00

I want to increment integer members of an object using Interlocked.Increment, but I want

  • 0

I want to increment integer members of an object using Interlocked.Increment, but I want to reference those integers via reflection. Example code I have, which is not working, is below.

public class StatBoard
{

    #region States (count of)
    public int Active;
    public int Contacting;
    public int Polling;
    public int Connected;
    public int Waiting;
    public int Idle;
    #endregion

    protected IEnumerable<FieldInfo> states;

    public StatBoard()
    {
        Type foo = GetType();
        FieldInfo[] fields = foo.GetFields(BindingFlags.Instance & BindingFlags.Public);

        states = from n in fields
                     where n.FieldType == typeof(int)
                     select n;

    }

    public void UpdateState(string key)
    {
        FieldInfo statusType = states.First( 
            i => i.Name == key
        );

        System.Threading.Interlocked.Increment(ref (int)statusType.GetValue(this));
    }

}

How do I modify the UpdateState method to make this work?

  • 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-15T20:46:51+00:00Added an answer on May 15, 2026 at 8:46 pm

    This cannot work by design. An int is a value type. The GetValue() method returns a copy of the int. You’ll increment that copy, not the original. Reflection doesn’t have any way to get a reference to a value type value.

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

Sidebar

Related Questions

I need to increment an integer, but I want to ease the speed at
I want to increment an unsigned integer from multiple threads. I know about Interlocked.Increment,
I want to Use Progressbar1.Increment but I wanna use Integer but the problem is
I want to increment id by 1 but I there is a problem while
Let's say I have a Set of Integers, and I want to increment every
I have total partitions of an integer and I want only those partitions which
I want to store a set of integers that get auto incremented at build
I want to increment a cookie value every time a page is referenced even
I want to increment the value of i . The for loop does not
I have several files containing this line Release: X I want to increment X

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.