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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:14:43+00:00 2026-06-14T00:14:43+00:00

In the following code, I am trying to have a method(Work) from class TestClass

  • 0

In the following code, I am trying to have a method(Work) from class TestClass change the values of some variables in the main program without having to return them. The variables are passed by reference in the TestClass constructor.

class Program
{
    static void Main(string[] args)
    {
        int a, b, c, d;
        a = 5; b = 10; c = 20; d = 25;
        Console.WriteLine("Main before TestClass: a=" + a + " b=" + b + " c=" + c + " d=" + d);
        TestClass testObj = new TestClass(ref a,ref b,ref c,ref d);
        testObj.Work();
        Console.WriteLine("Main after TestClass: a=" + a + " b=" + b + " c=" + c + " d=" + d);
        Console.ReadLine();
    }
}

public class TestClass
{
    int _a, _b, _c, _d;
    public TestClass(ref int a, ref int b, ref int c, ref int d)
    {
        _a = a; _b = b; _c = c; _d = d;
    }

    public void Work()
    {
        Console.WriteLine("Work before changing: a=" + _a + " b=" + _b + " c=" + _c + " d=" + _d);
        _a = 0; _b = 1; _c = 2; _d = 3;
        Console.WriteLine("Work after changing: a=" + _a + " b=" + _b + " c=" + _c + " d=" + _d);
    }
}

However this code returns :

Main before TestClass: a=5 b=10 c=20 d=25
Work before changing: a=5 b=10 c=20 d=25
Work after changing: a=0 b=1 c=2 d=3
Main after TestClass: a=5 b=10 c=20 d=25

Is there any way to have the method change the values of the variables in the Main program?
Thank you!

  • 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-14T00:14:44+00:00Added an answer on June 14, 2026 at 12:14 am

    You would be better off creating your own wrappers over Int32 in order for changes to be reflected, because once the values are assigned to fields of the class, they are no longer references, but rather different instances of Int32. Consider the following code:

    class Integer {
        public int Value;
    
        public Integer(int value) {
            Value = value;
        }
    
        public override string ToString() {
            return Value.ToString();
        }
    }
    
    class TestClass {
        Integer _a, _b, _c, _d;
    
        public TestClass(Integer a, Integer b, Integer c, Integer d) {
            _a = a;
            _b = b;
            _c = c;
            _d = d;
        }
    
        public void Work() {
            _a.Value = 111;
            _b.Value = 222;
            _c.Value = 333;
            _d.Value = 444;
        }
    }
    

    So now you have an Integer — a wrapper class over Int32. The usage will bring you the results:

    Integer a = new Integer(0), b = new Integer(0), c = new Integer(0), d = new Integer(0);
    Console.WriteLine("a: {0}, b: {1}, c: {2}, d: {3}", a, b, c, d);
    new TestClass(a, b, c, d).Work();
    Console.WriteLine("a: {0}, b: {1}, c: {2}, d: {3}", a, b, c, d);
    

    The output is:

    a: 0, b: 0, c: 0, d: 0
    a: 111, b: 222, c: 333, d: 444
    

    You may also find it useful to read more about classes and structs in C#, e.g. http://msdn.microsoft.com/en-us/library/ms173109.aspx . (Int32 is a struct, whereas in your case you probably need a class)

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

Sidebar

Related Questions

I have the following code that i am trying to change from a regular
I have been trying to extract e-mail addresses from the following code for quite
I have the following code for adding to/extracting from Zip. I'm trying to refactor
I have been trying to get the following code to work(everything is defined in
I have some legacy code with linq2sql classes. DataContext class has following definition for
Hi I am hitting std::out_of_range: basic_string::substr in the following code. Have been trying hard
I have the following code that I am trying to extract the systems proxy
I have the following code sample that im trying to wrap my head around
I have the following code. I am trying to bind the Command property of
I have the following code, The problem is when I'm trying to assign the

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.