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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:27:42+00:00 2026-06-15T11:27:42+00:00

I have a variable that I’m trying to set with Class1.SetVariable(variable); but it won’t

  • 0

I have a variable that I’m trying to set with Class1.SetVariable(variable); but it won’t return the variable I set it to. The variable will equal 0 in the other function in Class2, not the 50 that I want. Any ideas? Code:

Updated:

class Class1 {
    public int rndk = 0;

    public int Rndk {
        get { return this.rndk; }
        set { this.rndk = value; }
    }
}

class Class2 {
    public Class1 instance = new Class1();

    public Class2() {
        Load();
        Check();
    }

    public void Load() {
        instance.Rndk = 50;
        Console.WriteLine(instance.Rndk);
        // returns 50
    }

    public void Check() {
        Console.WriteLine(instance.Rndk);
        // returns 0
    }
}

That’s pretty much the code.

  • 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-15T11:27:43+00:00Added an answer on June 15, 2026 at 11:27 am

    First off, in C#, it’s typically better to make this a Property instead of a field with a “set method”:

    private int variable = 0;
    public int Variable 
    { 
       get { return this.variable; }
       set { this.variable = value; }
    }
    

    This is essentially two methods, but wrapped in much nicer syntax.

    As for the actual issue, I suspect the problem is that your methods aren’t working on the same instance of the Class1 class. Make sure you’re using the same instance in your Class1 variable, and not creating a new instance each method call. (Your existing code doesn’t demonstrate the actual problem.)

    For example, with the above change, this will work:

    public class Class2
    {
        private Class1 instance1 = new Class1();
    
        public Class2()
        { 
            instance1.Variable = 50;
        }
    
        public void Print()
        {
             // Using the same instance - will print 50
            Console.WriteLine("Instance value is {0}", instance1.Variable);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable that I'm using to build a JavaScript function call, and
I have a variable that I am trying to examine its content that is
I have a variable that will have the following values: blah = # 1,
I have a variable that is an array in Smarty, and I am trying
I have variable that contains a number. While Lua allows variables to be set
I have a variable that gets sent to a email as text but the
I have a variable name that I pass into a plugin, but the variable
I have a variable that I want to return a max of 5 to
I have variable x that contains set of attributes. var x = 'class=test test1
I have a variable that gets a value in a js function. I need

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.