CountZero
CountZero

Reputation: 6379

Returning a reference to an object

I'm trying to return a reference to an object, not pass it by value. This is due to not being able to pass a property by reference.

How can I get the code below to write 'new number plate' not 'old number plate'. After a bit of looking it seems like I cant. Just wanted to be sure first. Seems odd you cant.

Cheers

Steve

http://hardcode.ro/archive/2008/03/18/c-return-reference-types.aspx

Link

 public partial class Test2 : DefaultPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var car = new Car();
            var numberPlate = car.GetNumberPlate();
            numberPlate = "new number plate";
            Response.Write(car.GetNumberPlate());
        }
    }
}


public class Car
{
    private string _numberPlate;

    public Car()
    {
        _numberPlate="old number plate";
            
    }

    public string NumberPlate
    {
        get { return _numberPlate; }
        set { _numberPlate = value;}
    }
    public string GetNumberPlate()
    {
        return _numberPlate;
    }
}

Upvotes: 1

Views: 558

Answers (6)

Steve Cooper
Steve Cooper

Reputation: 21470

Are you looking to pass the property the same way you might pass a method into a function? For methods you'd do something like this;

// fn takes a method which returns string, calls it, and prints it.
void PrintsAMethodsResult(Func<string> GetString)
{
  Console.WriteLine( GetString() );
}

// calls like this;
PrintsAMethodResult( Car.GetNumberPlate );

But if you want to do it with a property, you've got a bit of a mess; properties are really just two methods (a getter and a setter, of type Func<T> and Action<T>) so you have to write different things do deal with the setter and the getter. A getter property can be called like this;

PrintsAMethodResult ( () => Car.NumberPlate );

This works the same as the GetNumberPlate method, by creating a function which takes no parameters and returns the car's number plate, effectively converting the property into a function.

There is no nicer way of passing the getter property as though it were a method.

Upvotes: 0

dr. evil
dr. evil

Reputation: 27265

Create another class within "Car" and then return it. That should do just fine. Although possibly you don't want to this since it's not a good way to code.

Upvotes: -1

hugoware
hugoware

Reputation: 36397

You're doing this a little backwards. If you want to change the value for that you need to actually change the property on the object.

car.NumberPlate = "old number plate";
Response.Write(car.GetNumberPlate());

If you're wanting to work with references you can use the ref keyword. This may not be what you're looking for though.

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1499770

You need to become clearer in your mind about the difference between objects and references. Your property already returns a reference to the string containing the current numberplate. It can't possibly return the object itself, because string is a reference type. (Even if you used a value type, the property would only be returning a copy of the data, so change to the variable's value still wouldn't affect the property.)

What you seem to want isn't a reference to the object but a reference to the property. There's no way of doing that in C# - and this is a good thing in my view, as it promotes readability. The kind of code you're trying to write in your example is much more complicated to understand, because assigning a value to a normal variable could do anything.

For more information, see my article about value types and reference types.

Upvotes: 4

Martin Harris
Martin Harris

Reputation: 28617

Since strings are immutable you can't do this. The value that you get back in this line:

var numberPlate = car.GetNumberPlate();

Is a reference to all intents and purposes (in that the value of the string is not copied to a different memory address), but this line

numberPlate = "new number plate";

Is semantically the same as:

numberPlate = new string("new number plate");

Which causes that reference to point to a different string.

Upvotes: 3

Jamie Ide
Jamie Ide

Reputation: 49251

     protected void Page_Load(object sender, EventArgs e)
    {
        var car = new Car();
        car.NumberPlate = "new number plate";
        Response.Write(car.NumberPlate);
    }

Your GetNumberPlate() method seems superfluous.

If you're using C# 3 or 3.5, I would rewrite this as:

    public partial class Test2 : DefaultPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var car = new Car();
            car.NumberPlate = "new number plate";
            Response.Write(car.NumberPlate);
        }
    }


public class Car
{

    public Car()
    {
        NumberPlate="old number plate";
    }

    public string NumberPlate { get; set; }

}

Upvotes: 2

Related Questions