views:

16

answers:

1

I have a whole slew of database access functions which assume a particular connection string. Within my application I call

myList = DB_MyTable.GetTableItems()

and within GetTableItems() I have something like

Dim connection As SqlConnection = MyDB.GetConnection

So the connection string is in one place in the code, and I call a method to get it.

What I'm running into now is I want to reuse the same database functions, but with a different connection string. I can rewrite all of the functions like DB_MyTable.GetTableItems() easily because they're generated from a script, but within the main application code I'll need to take care of every function call that now needs to know what connection string I want to use.

I tried changing the arguments to GetTableItems() like this:

Public Shared Function GetTableItems(Optional ByVal useThisString as String = MyDB.GetConnection) As List(Of MyItems)

in hopes of being able to pass in, by default, the string I'm already using in most of the code, but I got an error saying that the default value had to be a constant expression. This would mean peppering a specific connection string everywhere, which I don't want to do.

Is there a way to accomplish what I'm after, or do I need to make the connection string a required argument and change all of the calls in my application to match the new signature?

Thanks as always!

+1  A: 

Can you make your default value an empty string? Then, in your functions, if the useThisString variable is blank, then use default, else use the one you passed in? A littler dirtier, but just barely.

Tommy
That did occur to me! Maybe I shied away from it initially because every single call would have that check at the beginning, and it might be a performance drag. But that would save me most of the work in retrofitting the existing code.
John at CashCommons
I ended up doing this, and it works fine. Thanks!
John at CashCommons