user1527484
user1527484

Reputation: 67

c# class variable still null after calling function

After calling func1 variable mydata stays null. In debug mode I see that in func3 it sets data to string. why it doesn't pass value after exiting function?
Class example

class myclass
{
    public string mydata;

    public int func1()
    {

        //....
            func2(/**/, mydata);
        //....
        return 1;
    }


    private int func2(/**/,data)
    {
        byte[] arr = new byte[1000];
            //...
                func3(arr,data);
            //...
        return 1;
    }


    private void func3(byte[] arr, string data)
    {
        char[] a = new char[100];
        //...

        data = new string(a);
    }

}

Upvotes: 0

Views: 3302

Answers (4)

dckrooney
dckrooney

Reputation: 3121

You're passing the reference to the string mydata by value. This means mydata will still refer to the same object after the function returns, regardless of what you do inside the function. If you wanted to change the string mydata, you could pass the reference by reference:

public int func1()
{
    //....
        func2(/**/, ref mydata);
    //....
    return 1;
}

private int func2(/**/, ref string data)
{
    byte[] arr = new byte[1000];
        //...
            func3(arr, ref data);
        //...
    return 1;
}

private void func3(byte[] arr, ref string data)
{
    char[] a = new char[100];
    //...

    data = new string(a);
}

Upvotes: 0

user854301
user854301

Reputation: 5483

It because all arguments are passed by reference into the method. data = new string(a); crates new instance of a string with new reference.

var o = new object(); // reference 1

function void method(object something)
{
   // here we have reference to something in stack, so if we will assign new value to it, we will work with stack copy of a reference.
   something = null; // we removed reference to something in method not initial o instance
}

Upvotes: 0

Dennis Traub
Dennis Traub

Reputation: 51624

First, it's an instance variable, not a class variable. To be a class variable it would have to be declared static.

Second, why do you hand it down as an argument to the respective functions? The way you're doing it, it creates a separate string in every method and doesn't refer to the original one. Just go ahead and call it directly:

private void func3(byte[] arr)
{
    //...
    mydata = new string(a);
}

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292345

By default, parameters are passed by value; it means that what is passed is actually a copy of the variable (which is a copy of the reference in the case of a reference type like string). When func3 assigns data, it only modifies a local copy of the variable.

Now, if you change func2 and func3 signatures so that data is passed by reference, you will get the expected result:

public int func1()
{

    //....
        func2(/**/, ref mydata);
    //....
    return 1;
}


private int func2(/**/,ref string data)
{
    byte[] arr = new byte[1000];
        //...
            func3(arr, ref data);
        //...
    return 1;
}


private void func3(byte[] arr, ref string data)
{
    char[] a = new char[100];
    //...

    data = new string(a);
}

I suggest you read Jon Skeet's article about parameter passing for more details.

Upvotes: 1

Related Questions