3

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 .

Honey
  • 169
  • 1
  • 8
  • Do you *only* want the classes to share this `Data` i.e. avoid making a public property? – doctorlove Nov 22 '17 at 13:11
  • Possible duplicate of [How do you pass an object from form1 to form2 and back to form1?](https://stackoverflow.com/questions/4887820/how-do-you-pass-an-object-from-form1-to-form2-and-back-to-form1) – Pio Nov 22 '17 at 13:12
  • *" I want another function in the Second class to access it"* - then second class need an instance of its *parent* (as member, e.g. `private` field). Btw, you have `NullReferenceException` right now. – Sinatr Nov 22 '17 at 13:15
  • yeah I didn't write the data class , its just to explain the idea – Honey Nov 22 '17 at 13:17
  • Why dont you write ``this.DataMember = data;`` in your ``Second_Function()``? – Rand Random Nov 22 '17 at 13:19
  • Rand does this mean that DataMember in the second class well Point the same DataMember in the First class ? – Honey Nov 22 '17 at 13:21
  • @Honey since you used ``ref``, yes it does – Rand Random Nov 22 '17 at 13:25
  • @RandRandom: Assuming `Data` is a class, that's got nothing to do with using `ref`. – Jon Skeet Nov 22 '17 at 13:28
  • @JonSkeet - assuming he has the mentioned line in place, if I am not mistaken, if he would say ``instanceOfFirst.DataMember = new Data();`` the ``Second.DataMember`` would change the instance aswell or? – Rand Random Nov 22 '17 at 13:31
  • @RandRandom: No, that wouldn't change `Second.DataMember` at all. The `DataMember` field is just a regular field - it doesn't matter whether it was initialized with a value taken from a `ref` parameter. – Jon Skeet Nov 22 '17 at 13:32
  • @JonSkeet - thought he would keep the reference "intact", than sorry for spreading nonsense – Rand Random Nov 22 '17 at 13:33
  • @JonSkeet - just out of curiosity would there be a way to do that? – Rand Random Nov 22 '17 at 13:34
  • @RandRandom: This is why it's important to distinguish between *a reference* and when a variable is *passed by reference*. But no, you can't create a "ref field". (There's a lot more you can do with ref now than before, and more coming in C# 7.2, but not that as far as I'm aware.) – Jon Skeet Nov 22 '17 at 13:38
  • @Honey replace without effecting the main object? if so, you can use Cloning – Monah Nov 22 '17 at 13:44

1 Answers1

4

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

Monah
  • 6,714
  • 6
  • 22
  • 52
  • 1
    "In c# all data are referenced" - that's a bad start. it's important to understand the difference between reference types and value types, and what `ref` does. (Assuming the reader would expect the wrong result isn't great either.) – Jon Skeet Nov 22 '17 at 13:29
  • 1
    I meant by this phrase what I included in my example, you might put `a = b.clone()`, here you are not referencing it, sorry my english – Monah Nov 22 '17 at 13:33
  • 1
    Right, so there are plenty of subtleties. Several ways to change data: a) changing a reference type field value to refer to a different object; b) changing a value type field value to a different value; c) changing a value within the object a reference type field value refers to; d) mutating a value type stored in a field. All the more reason to avoid massive blanket statements such as "In c# all data are referenced". That doesn't help anyone IMO. – Jon Skeet Nov 22 '17 at 13:35
  • 1
    Thank your for the great comment, I updated my answer and hope now it will be more clear for the reader and forgive my mistake :P – Monah Nov 22 '17 at 13:42