0

Greetings everyone
I was wondering if i have bool x = true; and i call a method after that might change x value. would this change apply if the method is void & don't return the result.
Example :

bool x = true;
string x = "";
Change_value(x);

if (!(x))
   x = "Ok";

would x value change when i call my method or do i need to do it as x = change_value(x)

The New Guy
  • 71
  • 1
  • 8
  • What method Change_value does? – Backs Jul 30 '15 at 14:34
  • 2
    I think you need to think through your example, you define `x` twice, which the C# compiler won't let you do. Its not clear what you are trying to do here, but no, the way you wrote it the change won't be reflected after the call, as your answers say, value types need to be passed by `ref` for the function to be able to change them and be reflected at the calling site. – Ron Beyer Jul 30 '15 at 14:37

6 Answers6

1

This depends on the type of the variable you are passing to a method. In your example no.

Value types will not see changes reflected in the calling scope, where properties on reference values will.

This answer explains the concept well

Note that you can use the ref keyword to have value types updated like reference types also.

You can change members of reference values, but you cannot reassign them.

namespace ConsoleApplication1
{
    public class TestObj
    {
        public string Val { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var testObj = new TestObj { Val = "Initial instance" };

            Console.WriteLine(testObj.Val);

            AssignNewInstance(testObj);

            Console.WriteLine(testObj.Val);

            // Prints: 
            // 'Initial instance'
            // 'Changed in AssignNewInstance'
            // 'Initial instance'
        }

        public static void AssignNewInstance(TestObj testObj)
        {
            testObj = new TestObj { Val = "Changed in AssignNewInstance" };

            Console.WriteLine(testObj.Val);
        }
    }
}`
Community
  • 1
  • 1
gbro3n
  • 6,729
  • 9
  • 59
  • 100
  • 1
    *"where reference values will"* Not entirely true, you can change members of reference values, but you cannot reassign them (aka, `passedObject = new SomeObject();`). You can only mutate it, not reassign it. – Ron Beyer Jul 30 '15 at 14:38
  • To be completely accurate, we should say that the variable can be reassigned, though the newly assigned instance will not be visible to the caller. – gbro3n Jul 30 '15 at 15:00
  • You can clarify if you want but the question was in regards to the value at the calling site, not really internal in the method. Go ahead for clarity sake. – Ron Beyer Jul 30 '15 at 15:02
1

I think what you might be looking for is passing a value type (bool) by reference. You can do this using the ref keyword. Here is the relevant excerpt:

The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the called method is reflected in the calling method.

So, it should change the variable if you change your code to this (add the "ref"):

bool x = true;
string x = "";
Change_value(ref x);

if (!(x))
   x = "Ok";

And then the Change_value would be declared like this:

void Change_value(ref bool someBool) { //do whatever }
  • This is the best answer, but needs some cleanup of the code portions. Upvoting for now. – mgnoonan Jul 30 '15 at 14:43
  • Hah...I didnt realize the string part of the question...scanned and saw what I expected to see I suppose. I'll try to edit it to address the misapprehension as well. –  Jul 30 '15 at 16:03
0

you have to use the ref prefix in the method parameters

void Change_value(ref Boolean x)
{
...
}

and call it like this:

Change_value(ref x);

you can go for MS explain: https://msdn.microsoft.com/en-us/library/14akc2c7.aspx

lem2802
  • 1,152
  • 7
  • 18
0

bool is a primitive, so it's passed by value (a copy passed to change_value). If you want to change it's value in the method you can use the Boolean class, like that:

Boolean x = true;
string x = "";
Change_value(ref x);

if (!(x))
x = "Ok";
Felix Av
  • 1,254
  • 1
  • 14
  • 22
0

Unless you pass value X by reference the value of x should not change.

nayef harb
  • 753
  • 1
  • 10
  • 19
0

You can define b as a field inside your class. Once you did that, you just have to assign it and the new value will be available from that moment on.

Here is a short example:

public static class Program
{
    private static bool b;
    public static void Main()
    {
        b = true;
        Console.WriteLine(b);
        DoStuff();
        Console.WriteLine(b);
    }
    private static void DoStuff()
    {
        b = false;
    }
}

This way you can access and modify b from anywhere inside your class.

Richtenzorg
  • 178
  • 1
  • 1
  • 9