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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:58:09+00:00 2026-05-15T06:58:09+00:00

Possible Duplicate: How do I assign by “reference” to a class field in c#?

  • 0

Possible Duplicate:
How do I assign by “reference” to a class field in c#?

Hello everyone – tell me how to make this work? Basically, I need an integer reference type (int* would work in C++)

class Bar
{
   private ref int m_ref;     // This doesn't exist

   public A(ref int val)
   {
      m_ref = val;
   }

   public void AddOne()
   {
      m_ref++;
   }
}

class Program
{
   static void main()
   {
      int foo = 7;
      Bar b = new Bar(ref foo);
      b.AddOne();
      Console.WriteLine(foo);    // This should print '8'
   }
 }

Do I have to use boxing?

Edit:
Perhaps I should have been more specific. I’m writing a BitAccessor class, that simply allows access to individual bits. Here’s my desired usage:

class MyGlorifiedInt
{
   private int m_val;
   ...
   public BitAccessor Bits {
      return new BitAccessor(m_val);
   }
}

Usage:

MyGlorifiedInt val = new MyGlorifiedInt(7);
val.Bits[0] = false;     // Bits gets a new BitAccessor
Console.WriteLine(val);  // outputs 6

For BitAccessor to be able to modify m_val, it needs a reference to it. But I want to use this BitAccessor many places, with just a reference to the desired integer.

  • 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-15T06:58:10+00:00Added an answer on May 15, 2026 at 6:58 am

    You can’t store a reference to an integer like that directly, but you can store a reference to the GlorifiedInt object containing it. In your case, what I’d probably do is make the BitAccessor class nested inside GlorifiedInt (so that it gets access to private fields), and then pass it a reference to this when it’s created, which it can then use to access the m_val field. Here’s an example which does what you’re looking for:

    class Program
    {
        static void Main(string[] args)
        {
            var g = new GlorifiedInt(7);
            g.Bits[0] = false;
            Console.WriteLine(g.Value); // prints "6"
        }
    }
    
    class GlorifiedInt
    {
        private int m_val;
    
        public GlorifiedInt(int value)
        {
            m_val = value;
        }
    
        public int Value
        {
            get { return m_val; }
        }
    
        public BitAccessor Bits
        {
            get { return new BitAccessor(this); }
        }
    
        public class BitAccessor
        {
            private GlorifiedInt gi;
    
            public BitAccessor(GlorifiedInt glorified)
            {
                gi = glorified;
            }
    
            public bool this[int index]
            {
                get 
                {
                    if (index < 0 || index > 31)
                        throw new IndexOutOfRangeException("BitAcessor");
                    return (1 & (gi.m_val >> index)) == 1; 
                }
                set 
                {
                    if (index < 0 || index > 31)
                        throw new IndexOutOfRangeException("BitAcessor");
                    if (value)
                        gi.m_val |= 1 << index;
                    else
                        gi.m_val &= ~(1 << index);
                }
        }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Okay, so basically I
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I'm very confused about
Possible Duplicate: Assign a value to class variable is assiging it for all instances
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Integer summing blues, short += short problem I feel dumb that this
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Difference between class property mVar and instance variable self.mVar I am new

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.