Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I Use a Method with Parameters or a Function?
    text
    copied!<p>I've got a philosophical programming problem. Let's say I have a class named Employees. Employees has business members that get set from a dataTable. In order to fill this, I use a method that takes an instance of the employee class, loops through a dataTable, and sets the members of the instance passed into it. For instance:</p> <pre><code>public void GetEmployees(objEmployee) { //the function I am calling returns a dataTable of all the employees in the db. dim dt as DataTable = dbEmployees.GetEmployees(); foreach(DataRow drow in dt.rows) { objEmployee.Name = drow["Name"].ToString(); objEmployee.ID = drow["ID"].ToString(); } } </code></pre> <p>Then I would call the code like this in my UI logic:</p> <pre><code>public void GetEmployees() { Employees employee = new Employees(); employee.GetEmployees(employee); } </code></pre> <p>My question is, is it acceptable to pass in my class instance into a method and change the properties like I am doing, or would it be more object-oriented to do it through a function like this:</p> <pre><code> public Employees GetEmployees() { Employees objEmployee = new Employees(); //the function I am calling returns a dataTable of all the employees in the db. dim dt as DataTable = dbEmployees.GetEmployees(); foreach(DataRow drow in dt.rows) { objEmployee.Name = drow["Name"].ToString(); objEmployee.ID = drow["ID"].ToString(); } return objEmployee } </code></pre> <p>And then I would call it like this:</p> <pre><code>private void GetEmployees() { Employees employee; employee = employee.GetEmployees(); } </code></pre> <p>Is there any advantage of using a function over a method? Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload