tags:

views:

550

answers:

7

Hey

I'm currently playing about with some XNA stuff learning to program AI. Anyway, here's my situation: Class A has a function which takes a reference to an instance of class B, does some stuff to to it, and returns it. Class B contains an instance of Class A, and then calls the function from it.

Example in code:

Class A
{
    B classB;

    public A()
    {
        classB = new B();
    }

    public void Act()
    {
        this = B.Do(ref this);
    }
}

Class B
{
    public A Do(ref A classA)
    {
        //Manipulate
        return classA;
    }
}

I've tried passing a memberwise clone .. but that didn't work, obviously, because "this" is read-only. I've no idea with this. I'm really stuck. Does anybody have any ideas? I'd ideally like to avoid having to pass every single variable in the object as a separate argument, really.

Andy.

A: 

Is there something specific in //Manipulate that requires you to pass a class reference as ref? And why are you also returning classA as well - that seems to be redundent if you are using ref to begin with.

David
The whole thing is for behaviours for simple AI (EG chase, guard, evade etc). I'm wanting to pass the entire AI entity to the behaviours so that I can keep the behaviours separate for all entities (Sort of like a single style sheet for multiple style sheets).The reason I want to pass the whole object is that some behaviours need certain parts of the object and some don'tAs for why I'm returning .. I don't quite know how I came to that decision. I'll remove it.
Single style sheet for multiple pages rather!
A: 

just remove the ref keyword. you only need ref if you want to change the argument into something else, if you just want to manipulate it, don't use the ref.

I think it would be bad (tricky to understand) if the language allowed you to change this into that, so it disallows what you've tried.

Scott Langham
A: 

you cannot use "ref" with "this". Why do you need the parameter to be passed by reference ?

You only need a "ref" parameter if your function must change which object the caller is refering to. If all you need is to manipulate and/or change the fields of the parameter then you do not need a ref parameter.

Denis Troller
A: 

For more clarity, the whole thing is for behaviours for simple AI (EG chase, guard, evade etc). I'm wanting to pass the entire AI entity to the behaviours so that I can keep the behaviours separate for all entities (Sort of like a single style sheet for multiple web pages). The reason I want to pass the whole object is that some behaviours need certain parts of the object and some don't.

As for why I'm returning .. I don't quite know how I came to that decision. I'll remove it

+2  A: 

Classes are reference types, so doing

Class B
{
    public void Do(A classA)
    {
        //Manipulate
    }
}

should manipulate the object classA references. Then in A,

Class A
{
    B classB;

    public A()
    {
        classB = new B();
    }

    public void Act()
    {
        B.Do(this);
    }
}

Note: "This does have the side effect that the reference of A that you pass cannot be set to null (it will only set the local variable to null)" - JulianR

Derek E
This does have the side effect that the reference of A that you pass cannot be set to null (it will only set the local variable to null), which might have been the original reason why it expects a ref parameter.
JulianR
That's a good point. I didn't think that was the OP's intent (it wasn't), but it could have been. I'll edit to point this out.
Derek E
A: 

Thank you very much, Derek E!

I just tested what you said, and it worked fine.

My problem, I think, is that I was thinking that when I passed the argument it made a clone of it and manipulated that instead, like (I think ..) it does in PHP or VB (can't remember which of those I was told it does it in)

A: 

Objects are always passed by reference and not by value. That is why we call classes in C# as reference types. Be careful if you are using structures instead since they are value types. A copy of the structure will be passed rather than the original. If you wish to pass the original then you must add the ref keyword. ref is only useful with objects if you wish to replace a reference to one object with a reference to another object.

anonymous