14 Replies - 6468 Views - Last Post: 22 June 2011 - 11:14 AM Rate Topic: -----

#1 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Pass by ref or return a new instance or out?

Posted 22 June 2011 - 06:29 AM

Since C# is a managed language I've got a question.

Is it better to pass an object by reference or simply return an instance?
class A
{
	private int x;
	public A() { } //let ignore using the constructor to initialize x
	public void setx(int x) { this.x = x; }
}


Examples
public void example_one(ref A a)
{
	a.setx(10);
}


public A example_two()
{
	A a = new A();
	a.setx(10);
	return a;
}



Would example two cause memory leaks and a huge application to have a lower performance?

public void example_three(out A a)
{
	A a2 = new A();
	a = a2;
}



Which approach would held the best performance of the three?

Is This A Good Question/Topic? 0
  • +

Replies To: Pass by ref or return a new instance or out?

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 06:41 AM

It depends on the situation entirely.

I personally think for most situations example 2 is best. It won't cause memory leaks, because when all references to the return value is lost the GC will destory the object from the heap in it's next sweep or 2. Example 3 is just creating work for yourself, but out can be incredibly useful - especially when you need to validate something - check Int32.TryParse for example. Example one should only be used when you need to pass the reference to a value, say if in the body of the method you want to modify the same integer value you pass in as an argument.
Was This Post Helpful? 2
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 06:58 AM

Thanks, that cleared up a few misconceptions I had.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 09:00 AM

Avoid both ref and out, if you can. You need them for primitives in certain cases. For objects, very very rarely. I wouldn't trust code that required such things of my object. What the hell are they trying to do?

If I pass a method an object, I kind of expect to get the same one back. If the method gives me a new instance, it can return it. If the method wants to offer two new instances, it's time to reconsider the method.
Was This Post Helpful? 1
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 09:21 AM

@baavgai What if you've got an object that handles the connection to a database and if one method closes it then it should be closed throughout your entire program. Won't ref be your best option in that instance?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 09:31 AM

View Postcodeprada, on 22 June 2011 - 12:21 PM, said:

@baavgai What if you've got an object that handles the connection to a database and if one method closes it then it should be closed throughout your entire program. Won't ref be your best option in that instance?


You really shouldn't be reusing connections across an application. Let the DB provider handle the connections using it's connection pooling. Don't try to handle it yourself.
Was This Post Helpful? 1
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 09:39 AM

Oh I see. So each Form should have it's own connection. I really should have switched to this implementation after changing my database to MySQL from an Access database file that locks the file on each connection :mellow: . I think that's why I had to pass a connection between Forms.

Thanks.

This post has been edited by codeprada: 22 June 2011 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 09:43 AM

View Postcodeprada, on 22 June 2011 - 12:39 PM, said:

Oh I see. So each Form should have it's own connection. I really should have switched to this implementation after changing my database to MySQL from an Access database file that locks the file on each connection :mellow: . I think that's why I had to pass a connection between Forms.

Thanks.


No. Even less than that. Your connection should be created before you do your transaction, and should be closed when your transaction is completed. You should not keep it open for multiple connections.

Your forms should definitely NOT have their own connection. The forms should know nothing about the database transaction.

You should call a method. That method should make the database connection, do the query, then close the connection. The connection shouldn't be used outside of that method.
Was This Post Helpful? 1
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 09:46 AM

For databases, you should have any connection open for only as long as it's needed to complete a transaction. There should be a finally block for every individual connection that closes the open connection.

However, for the example in question, a connection object holds the connection. All methods of that object may be called by any method it's passed to. Why pass by ref?

To be clear, objects are always passed by reference, they are never copied. The ref keyword has meaning beyond that and there's really very little justification to use it a purely .NET program. It exists primarily for interop with old C++ code.

Edit: Note, never use Access for an application. Ever. It's simply not suitable.

This post has been edited by baavgai: 22 June 2011 - 09:47 AM

Was This Post Helpful? 1
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 10:16 AM

View Posteclipsed4utoo, on 22 June 2011 - 12:43 PM, said:

View Postcodeprada, on 22 June 2011 - 12:39 PM, said:

Oh I see. So each Form should have it's own connection. I really should have switched to this implementation after changing my database to MySQL from an Access database file that locks the file on each connection :mellow: . I think that's why I had to pass a connection between Forms.

Thanks.

Your forms should definitely NOT have their own connection. The forms should know nothing about the database transaction.

I should have said a wrapper class to handle the connection instead of the actual connection.

View Postbaavgai, on 22 June 2011 - 12:46 PM, said:

Edit: Note, never use Access for an application. Ever. It's simply not suitable.

:^:

So I've got some restructuring to do. I'd now have to put the MySQLConnection.Open() connection before the transactions and MySQLConnection.Close() just after.
Was This Post Helpful? 0
  • +
  • -

#11 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 10:28 AM

Another question for you guys

I'm using MySql.Data which is almost identical to OleDB as in it uses an Adapter and Command (maybe it's a derived class or implements the same interface idk) but which method actually does the query execution? .SelectCommand or .Fill?
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 10:31 AM

Or wrap it in a using statement, so that it closes and disposes of your connection at the end of it.

Also, I think you might be confused about ref and out, when dealing with types that are already reference types. If you've ever done any C/C++, the best way to describe it would be that you're passing a pointer to a pointer. Reference types are already pointers, so if you change their properties in a method, that change persists outside the method. The only reason to use a ref or out keyword is if your method needs to instantiate a new instance and have that reflected back to the original variable. Of course, you usually can just return that new reference, so it's usually not advisable to use them.

A common use is when you need two values back from a method. Like the TryParse methods on all the numeric types.

Also, a note on your original example code. You used a method called "setx". That's a very Java-ish way of doing things. C# has what's called Properties, so we don't need setter and getter methods. Look up Properties.

View Postcodeprada, on 22 June 2011 - 12:28 PM, said:

Another question for you guys

I'm using MySql.Data which is almost identical to OleDB as in it uses an Adapter and Command (maybe it's a derived class or implements the same interface idk) but which method actually does the query execution? .SelectCommand or .Fill?


SelectCommand is a property. In Java terms, it's both a setSelectCommand and getSelectCommand method. Fill is the method that goes to the database, does the select, and populates a data table in the data set.

And yes, they are derived from DbDataAdapter, and DbDataCommand (and DbConnection, etc...).
Was This Post Helpful? 1
  • +
  • -

#13 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 10:56 AM

View PostCurtis Rutland, on 22 June 2011 - 01:31 PM, said:

Also, a note on your original example code. You used a method called "setx". That's a very Java-ish way of doing things. C# has what's called Properties, so we don't need setter and getter methods. Look up Properties.

Yes, I've got them in my application :smile2: I think I'm down with the ref and out now.

Thanks again for all your help.

So in a nutshell I've now got this
  • Each form has it's own instance of my wrapper class MySQL
  • Elimination of ref and out on Objects
  • Connection opened before and closed after each transaction

Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 10:58 AM

In a garbage-collected language it's not necessarily important for each form to have an instance of that class. You can instantiate it on demand, and allow it to go out of scope and be GC'd. You don't have to stack a bunch of private fields into a form, really you only need what has to be used across methods.
Was This Post Helpful? 1
  • +
  • -

#15 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Pass by ref or return a new instance or out?

Posted 22 June 2011 - 11:14 AM

View PostCurtis Rutland, on 22 June 2011 - 01:58 PM, said:

In a garbage-collected language it's not necessarily important for each form to have an instance of that class. You can instantiate it on demand, and allow it to go out of scope and be GC'd. You don't have to stack a bunch of private fields into a form, really you only need what has to be used across methods.

That's even better. The main form has only three methods that require use of the class which is the most.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1