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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:53:58+00:00 2026-06-03T21:53:58+00:00

Is it possible to assign a variable to another variable and when you change

  • 0

Is it possible to assign a variable to another variable and when you change the second variable the change waterfalls down to the first variable?

Like this.

int a = 0;
int b = a;
b = 1;

now both b and a would = 1.

The reason I ask this is because I have 4 objects I’m keeping track of, and I keep track of each using a 5th object called currentObject that equals whichever of the 4 objects the user is using. However, I would like to just make a change to the currentObject and just have it waterfall to the variable it came from.

Thanks

  • 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-06-03T21:53:59+00:00Added an answer on June 3, 2026 at 9:53 pm

    You need to distinguish between objects, references and variables. If you have two different variables (which aren’t aliased via ref/out in a method, etc) then those will be independent.

    However, if two variables refer to the same object (i.e. their type is a class, and their values are equal references), then any changes to that object will be visible via either variable. It sounds like this is what you want to achieve. For example:

    public class SomeMutableClass
    {
        public string Name { get; set; }
    }
    
    // Two independent variables which have the same value
    SomeMutableClass x1 = new SomeMutableClass();
    SomeMutableClass x2 = x1;
    
    // This doesn't change the value of x1; it changes the
    // Name property of the object that x1's value refers to
    x1.Name = "Fred";
    // The change is visible *via* x2's value.
    Console.WriteLine(x2.Name); // Fred
    

    If you’re not entirely comfortable with how reference types and objects work, you may wish to read my article about them.

    EDIT: One analogy I often use is of a house. Suppose we have two pieces of paper (variables). The same house address is written on both pieces of paper (that’s the value of each variable, the reference). There’s only one house. If someone uses the first piece of paper to get to the house, then paints the door red, they’re not changing anything about their piece of paper – they’re changing something about the house. Then if someone uses the second piece of paper to get to the house, they’ll see the front door is red too. There’s only one house, however many pieces of paper have its address written on them.

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

Sidebar

Related Questions

is it possible to assign a variable a maths operator. this is what I've
Is it possible to assign some kind of variable that value of it would
Is it possible to assign the value a git command like git rev-list -n
Using pointer arithmetic, it's possible to assign characters from one array to another. My
Is it possible to assign a local variable a value whose scope is outside
for instance in python it is possible to assign a method to a variable:
is it possible to assign variable inside if conditional in bash 4? ie. in
It is not possible to assign an integer value to a reference variable directly,
I was wondering if it possible to assign occurrences of characters to variable until
Is it possible to create a variable in xslt and assign it the value

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.