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

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not to my knowledge. Consider leaving a comment on Linda… May 16, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer Having read your comments to Will's response I know Suits… May 16, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer Iterate through the array and count the things you're looking… May 16, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Possible Duplicate: what is the difference between #include <filename> and #include “filename” Why do
Possible Duplicate: How to assign Profile values? I'm using ASP.NET MVC and the Membership
Possible Duplicate: Idiomatic object creation in ruby Sometimes it's useful to assign numerous of
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: declare property as object? in java you can create an object directly
Possible Duplicate: How do I do multiple assignment in MATLAB? When dealing with cell
Possible Duplicate: What does the explicit keyword in C++ mean? explicit CImg(const char *const
Possible Duplicate: Specify Command for MenuItem in a DataTemplate I have a collection 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.