4 Replies - 520 Views - Last Post: 04 March 2013 - 06:51 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Does Creating An Object In A Function Create Two Objects?

Posted 04 March 2013 - 04:11 AM

If you have something like
public RichTextBox CreateRTB()
{
 RichTextBox r = new RichTextBox{arugments here};
return r;
}


and then you called it later like
var rtb = this.CreateRTB();



I believe you would only have one instance of a Rich text box stored within rtb or would you have two since it was called once within the function and returned? Is it safe to return objects within methods like this?

Is This A Good Question/Topic? 0
  • +

Replies To: Does Creating An Object In A Function Create Two Objects?

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Does Creating An Object In A Function Create Two Objects?

Posted 04 March 2013 - 04:23 AM

You've created just one object, as you guessed. You're then passing that back through return to the implicitly-typed "rtb" variable.

As for it being safe, it's not only safe, it's the way you should be doing it.
Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Does Creating An Object In A Function Create Two Objects?

Posted 04 March 2013 - 04:24 AM

No your just creating one object and passing it back from the method in the return value.

It's safe :)

Edit - ninja'd

This post has been edited by Ryano121: 04 March 2013 - 04:24 AM

Was This Post Helpful? 1
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Does Creating An Object In A Function Create Two Objects?

Posted 04 March 2013 - 06:01 AM

This may (or may not) make it clearer for you...

RichTextBox is a reference type. Meaning, when you use the new operator like this - RichTextBox r = new RichTextBox{arugments here}; - what happens is an object is created on the heap, and a reference to that object is returned to you, and stored in r.

Therefore, this line - return r; - is returning that reference. It isn't returning an object, meaning this line - var rtb = this.CreateRTB(); - is storing the reference to the created object in rtb.

You have to remember that you are only ever dealing with references when using reference types, not the actual objects themselves.

This post has been edited by CodingSup3rnatur@l-360: 04 March 2013 - 06:04 AM

Was This Post Helpful? 1
  • +
  • -

#5 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Does Creating An Object In A Function Create Two Objects?

Posted 04 March 2013 - 06:51 AM

Not to disagree with someone that has a lot more reputation than me and is probably a more experienced programmer but... I don't really like the metaphor of saying the object is "stored in" rtb. As someone still pretty new to programming, this confused me when I used to think about references that way.

References are just that - references. They're like labels. Here's an example. Imagine a house as an object.

public void CreateHouse()
{
    return new House();
}



There's a house object that's been created, but it doesn't have a name. It's just an instance of a House. To refer to it, you need a referece.

House myHouse = CreateHouse();



Now you can talk about the house. It's called "myHouse." We could also create more refernces:

theThirdWhiteHouseOnTheLeft = myHouse;
_123MockingbirdLane = myHouse;
thatOneHouseWithTheInGroundPool = myHoulse;



None of those "contain" the object. They're labels - references - to the object. And many objects both in code and in real life have different names depending on the context and who is doing the talking!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1