简体   繁体   中英

C# returning public field from method

I'm writing scripts for Unity3D in C# and have a little helper method in one of my classes called constraintForType that returns a public field for a certain type like so:

public Constraint topConstraint = Constraint.falseConstraint;
public Constraint bottomConstraint = Constraint.falseConstraint;

enum ConstraintType {
    Top,
    Bottom
}

Constraint constraintForType(ConstraintType type) {
    switch(type) {
    case ConstraintType.Top:
        return topConstraint;
    case ConstraintType.Bottom:
        return bottomConstraint;
    }

    return Constraint.falseConstraint;
}

I'm calling this method from the Update loop but it seems that the constraint that I'm returning is not the same as the public declaration (maybe a copy?).

So in the Update loop this would work:

void Update() {

    topConstraint.constant = // Calculated value
}

But this doesn't:

void Update() {

    Constraint con = constraintForType(ConstraintType.Top);
    con.constant = // Calculated value
}

I thought maybe the method was returning a copy of the public field so I changed it to:

void constraintForType(ConstraintType type, ref Constraint con) {

    switch(type) {
    case ConstraintType.Top:
        con = topConstraint;
        break;
    case ConstraintType.Bottom:
        con = bottomConstraint;
        break;
    }
}

So I then call this in the Update :

Constraint con = Constraint.falseConstraint;
constraintForType(ConstraintType.Top, ref con);

This still doesn't work though.

What is going on here?

EDIT:

Constraint is a struct.

Change your Constraint -struct to class. Structs are value types that are (usually) immutable.

You are right, your first method is returning a copy of the stuct. However, your second version is making a copy as well:

void constraintForType(ConstraintType type, ref Constraint con) {

    switch(type) {
    case ConstraintType.Top:
        con = topConstraint; // struct is copied here
        break;
    case ConstraintType.Bottom:
        con = bottomConstraint; // struct is copied here
        break;
    }
}

One way to make this work in your case would be to assign inside the helper function

void constraintForType(ConstraintType type, Foo constant) {

    switch(type) {
    case ConstraintType.Top:
        topConstraint.constant = constant; 
        break;
    case ConstraintType.Bottom:
        bottomConstraint.constant = constant; 
        break;
    }
}

Of course, if you do not have a particular reason to use a mutable struct, use a class instead. Mutable structs have some difficulties.

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