Jump to content
  • Advertisement
Sign in to follow this  
R4z0r1991

[.net] C# - returning a ref?

This topic is 3075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! In the following code snippet, shouldn't add() return a reference of the object this? Cause after entity = item, this still contains its default value. public class EntityCollection : List<E2D_Entity> { new public void Add( E2D_Entity item ) { E2D_Entity entity = add( item ); if ( entity != null ) entity = item; } private E2D_Entity add( E2D_Entity item ) { for ( int i = 0; i < this.Count; i++ ) { if ( this.IsDead ) return this; } base.Add( item ); return null; } } Thanks

Share this post


Link to post
Share on other sites
Advertisement
Add returns the value that was stored at this. Because E2D_Entity (strange naming btw) happens to be a class, this value is either null or a reference to an object of type E2D_Entity. Add will, however, not return a reference to the slot this itself.

Share this post


Link to post
Share on other sites
How come that this works then?

public partial class Form1 : Form
{

TC test = new TC();

private void Form1_Load( object sender, EventArgs e )
{
Test ts = new Test();
test.Add( ts );
test.get();
}

}

public class Test
{
public bool check = true;
}

public class TC : List<Test>
{

public void get()
{
Test ts = get2();
ts.check = false;
}

public Test get2()
{
return this[0];
}

}

Share this post


Link to post
Share on other sites
To me it looks like you're trying to replace an element at and you want to be doing it like so


if ( this.IsDead )
{
this = item;
return this;
}



Your second snippet works the same as the first, you're not changing what this[0] points to but the contents themselves.

The code below in effect does nothing, because you're assigning to a scoped variable and when it leaves the scope, nothing will happen. In essence, what VizOne said.


E2D_Entity entity = add( item );
if ( entity != null )
entity = item;

Share this post


Link to post
Share on other sites
Quote:
Original post by Niksan2
To me it looks like you're trying to replace an element at and you want to be doing it like so


Why didn't I think about that before? xP

Quote:
Your second snippet works the same as the first, you're not changing what this[0] points to but the contents themselves.

The code below in effect does nothing, because you're assigning to a scoped variable and when it leaves the scope, nothing will happen. In essence, what VizOne said.


Thanks, but something still confuses me. The second snippet get2() returns this[0] to get(), if it is like you said, the scoped variable ts in get() shouldn't be able to change this[0] content as you said that get2() couldn't return a ref to this[0].

Share this post


Link to post
Share on other sites
Quote:
Original post by R4z0r1991
Thanks, but something still confuses me. The second snippet get2() returns this[0] to get(), if it is like you said, the scoped variable ts in get() shouldn't be able to change this[0] content as you said that get2() couldn't return a ref to this[0].


That's because it doesn't return this[0] but the Test class that this[0] points to, and you can change it because ts is pointing to the same bit of memory that this[0] is.

The same way the following works:

Test t = new Test();
Test t2 = t;
t2.check = false;

t.check and t2.check would be false;

Share this post


Link to post
Share on other sites
His just overriting the variable.


new public void Add( E2D_Entity item )
{
E2D_Entity entity = add( item ); //entity now holds a reference to the object at this
if ( entity != null )
entity = item; //entity now holds a reference to the object referenced by item, nothing have happend to this
}



You cant overwrite a object by doing

E2D_Entity var1 = new E2D_Entity();
E2D_Entity var2 = new E2D_Entity();

var2 = var1;

You only overwrite the variable var2. Sure you now lost the object initialy used for var2 but the memory it occypied didnt get overwritten with a copy of the object at var1.

hope that made sense
[/source]

[Edited by - Lio on April 20, 2010 5:21:11 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Lio
His just overriting the variable.

*** Source Snippet Removed ***


It is not correct to use the term "address" the way you did in that snippet. Rather than say 'holds the address of' you instead say 'is a reference to.'

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:
Original post by Lio
His just overriting the variable.

*** Source Snippet Removed ***


It is not correct to use the term "address" the way you did in that snippet. Rather than say 'holds the address of' you instead say 'is a reference to.'


Yes i see your point. Now fixed :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!