简体   繁体   中英

Change by value reference in c#

I have RangeClass and a MyClass and it has RangeClass property.

 public class RangeClass{
    public int min;
    public int max;
 } 

and

 public class MyClass{
     RangeClass range;
     public void setRange(Range r){

     }
 }

I want to create several instances of MyClass and the myClass.range is a reference of a RangeClass(say, defaultRange). So whenever I change the defaultRange, it will reflect to all the range property of myclass. I want to find out how can I do this without going through each of the instances. I found this post , but I'm not sure how to modify the accepted answer to achieve my goal.

UPDATE: Code below shows two range classes that are used as property of MyClass classes and shows how to access the range method. public class Range{ public float min; public float max; public Range(float a, float b){ min = a; max = b; }

    public bool Contains(float a){
        if (a >= min && a<=max){
            return true;
        }
        return false;
    }
}

public class MyClass{
    public static Range range;
    public void SetRange(Range r){
        range = r;
    }
    public bool Cointains(float num){
        return range.Contains(num);
    }
}

public class OtherClass{
    MyClass instance1;
    MyClass instance2;

    public OtherClass(){
        Range range1 = new Range(1,5); //creates instance of Range
        Range range2 = new Range(6,10); //creates another instance of Range

        instance1 = new MyClass();
        instance1.SetRange(range1);

        instance2 = new MyClass();
        instance2.SetRange(range2);

        bool isInSetA = instance1.Contains(4);
        bool isInSetB = instance2.Contains(4);
    }
}

将范围设为MyClass的静态属性,以便所有MyClass对象共享同一范围。

This is normal behavior for reference types, since you're sharing single RangeClass instance between several instances of MyClass . If you don't want to reflect changes, then:

1 - you can do your RangeClass immutable, so, you couldn't change its instances at all:

public class RangeClass
{    
    public int Min { get; private set; }
    public int Max { get; private set; }

    public RangeClass(int min, int max)
    {
        Min = min;
        Max = max;
    }
}

2 - You can add a creation of default RangeClass instances at MyClass constructor:

public class MyClass
{
     public MyClass()
     {
         range = new RangeClass { min = 0, max = 100 };
     }

     RangeClass range;

     public void setRange(Range r)
     {
     }
 }

If you do want to reflect the changes, just made a public static property or public static readonly field in RangeClass , which will correspond to default range:

public class RangeClass{
    public int min;
    public int max;

    public static readonly RangeClass DefaultRange = new RangeClass { min = 0, max = 100 };
 } 

Isn't the static modifier what you want? http://msdn.microsoft.com/en-us/library/98f28cdx.aspx

Eg: Pseudo code

Class ClassA
{
    public int IntA = 0;
    public int IntB = 5;
}

class ClassB
{
    public static ClassA;
}

main
{
    ClassB MyClassB;
    ClassB MySecondClassB;

    MySecndClassB.ClassA.IntA = 1; //The vale of 1 will be overwritten by
    MyClassB.ClassA.IntA = 5; //the value of 5 here, for all instances of ClassB.
    System.Diagnostics.Print(MySecndClassB.ClassA.IntA.ToString() + " - " + MyClassB.ClassA.IntA.ToString());
}

Change your range property from instance to Static. This will allow RangeClass to be viewed from any of the MyClass instance with default value or modified state.

public class MyClass{
     public static RangeClass range;
     public void setRange(Range r){

     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM