pass function name as parameter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

trying to shorten up code by passing a function name as a parameter.

Here is what it looks like:
function doSomething(function myFunctionName()) {
// some code like:
// DB.createAdapter
//DB.attachAdapter
// DB.isOpen

// do this function - which does something different every time
// like fetch names the first time, fetch users the 2nd, fetch ID the 3rd,
etc...
myFunctionName();
}

this way, i can easily save 5-10 lines of db connection code by simply
passing in a new function instead of creating the adapter and connection over
and over.

Instead the new code would look like so:
// do DB code and run function passed
users = doSomething(fetchUsers());
names = doSomething(fetchNames());
ids = doSomething(fetchIDs());

is this possible? reason i want to do this is because i have so many copy
and pasted lines of the same code and instead of taking up so much space i'd
like to just use those lines of code before and after a function that does
something different!

please help. thanks in advance.
 
I would probably create a class that had fetchUsers(), fetchNames() and
fetchIDs() as methods. You could probably create a function in your code
that creates a database connection and returns the database connection as a
result; i.e. -

using System.Data.SqlClient;

private SqlConnection openConnection(string ConnectionString)
{
SqlConnection sqlcon = new SqlConnection (ConnectionString);
//insert additional code here
return (sqlcon);
}
 
Back
Top