Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference another class member

Tags:

c#

reference

ref

I have three classes (Data, First, Second ).

I'm creating member of Data in First and try to pass it to Second with the following:

public class First
{
    public Data DataMember;
    Second SecondMember;

    void First_Function()
    {
        SecondMember.Second_Function(ref DataMember);
    }
}

public class Second
{    
    Data DataMember;

    public void Second_Function(ref Data data)
    {

    }    
}

Is there a way to access the First.Data member in Second.Data member?

Using ref in the Second.Second_Function() allows me to access the member of the First but only inside the Second_Function().

I want another function in Second to access it, that has a different "call back time" as the Second_Function().

Edit :

My question in not about what is the difference between the reference and value type .

if I use ref keyword for a int variable , that mean if I replace it with another value it will effect the original .

in class when I have two variable reference to the same instance if I edit one of them I effect the other , that's because they reference to the same thing ,I want to know if there is a way in C# to replace one of the variable ,and make the other variable change with it .

like image 995
Honey Avatar asked Jun 07 '26 12:06

Honey


1 Answers

There are two kinds of types in C#: reference types and value types. Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable. With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other (except in the case of ref and out parameter variables, for more details see here

Here an example:

Explanation

  1. Pass your data object to the class First, Second as parameter in the constructor
  2. Any updates happens on the data object inside First or Second will be reflected on the data object outside these classes because it is reference type

    public class Data{ public Data(int value) { this.Value = value; } public int Value{get;set;} }

    public class First{ private Data m_data; public First(Data data) { m_data = data; } public void Add(int value) { if(m_data!=null) m_data.Value+=value; } }

    public class Second{ private Data m_data; public Second(Data data) { m_data = data; } public void Multiply(int value) { if(m_data!=null) m_data.Value*=value; } }

now let us setup this scenario

var data = new Data(10);
var first = new First(data);
var second = new Second(data);          
second.Multiply(5);         
first.Add(10);

What do you expect the value inside the class data? 10? you are wrong, the value is 60

Here a working demo

Hope this will help you

like image 159
Monah Avatar answered Jun 10 '26 06:06

Monah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!