Wine Too
Wine Too

Reputation: 4655

DB connection as optional parameter

VB.NET supports optional parameters as arguments to functions like such:

Public Function myfunction(ByVal myint As Integer, _
                Optional ByVal myoptint As Integer = 1)

That worked OK.
Default value for optional parameter is "MUST BE".
Regarding this I was trying to pass DB connection as optional parameter and don't know how to do it properly with "default value".

Public Function myfunction(ByVal myint As Integer, _
                Optional ByVal conn As ODBCConnection = WHAT??)

Purpose of that is that I can use external existed connection inside a function or if connection don't exist that program connects inside a function.
For example:

If conn Is Nothing Then ConnectToDB(dbName, dbUser, dbPass)  

So, how to write DBconnection As optional parameter properly?

Upvotes: 0

Views: 220

Answers (1)

ermagana
ermagana

Reputation: 1230

Have you tried

Public Function myfunction(ByVal myint As Integer, _
                Optional ByVal conn As ODBCConnection = Nothing)

Nothing is the default value for a class in VB.Net and as ODBCConnection is a class it's default value should also be Nothing.

Hope this helps

Upvotes: 2

Related Questions