Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optional Keyword / Overloading 1

Status
Not open for further replies.

Badgers

Programmer
Nov 20, 2001
187
0
0
US
Hi,

I have a function with a large number of variable arguments, it is public and therefore using the optional keyword is not recommended.

How else could this de done, overloading would be a nightmare.

Here is the functin :

Public Function GetPayments(ByVal MerchantCompanyName As String, ByVal InvoiceStatus As String, _
ByVal PayoutStatus As String, ByVal ProductName As String, _
ByVal AffiliateCompanyName As String, ByVal NETPayout As String, _
ByVal VATPayout As String, ByVal StartDate As String, _
ByVal EndDate As String, ByVal InvoicePayoutID As String, _
ByVal CountryID As System.Nullable(Of Integer), ByVal AgencyID As System.Nullable(Of Integer), _
ByVal CurrentPage As System.Nullable(Of Integer), ByVal PageSize As System.Nullable(Of Integer), _
ByRef TotalRecords As System.Nullable(Of Integer)) As DataTable

Using objadapter As New DAL.dsPaymentsTableAdapters.ta_InvoicePayoutStatsTableAdapter

Return objadapter.GetData(MerchantCompanyName, InvoiceStatus, PayoutStatus, ProductName, AffiliateCompanyName, _
NETPayout, VATPayout, StartDate, EndDate, InvoicePayoutID, CountryID, AgencyID, _
CurrentPage, PageSize, TotalRecords)

End Using

End Function

Thankd
 
hi,

why do you feel that optional is not recommended???

Known is handfull, Unknown is worldfull
 
Because in Microsoft's best practise they say only use it on private methods, also if c# tried to call a vb.net function with optional params, the c# code would need to set each param.

 
hmm,

then i guess you are stuck with overloading the methods. i will also try to do some research on the same...

Known is handfull, Unknown is worldfull
 
You might also create a class to contain the data in all those parameters, and just pass in an instance of that class.
For instance...
Code:
Public Class PaymentRequest

   Public Property MerchantCompanyName As String...
   Public Property InvoiceStatus As String ...
   ...etc

End Class
The Properties in PaymentRequest could have default values or remain empty as needed.

Now you can define the function like this:
Code:
Public Function GetPayments(ByVal forRequest as PaymentRequest) as DataTable
...
End Function

usage:
Code:
Dim myRequest As New PaymentRequest()
myRequest.MerchantCompanyName = "foo"
myRequest.InvoiceStatus = "bar"
'fill in whatever other properties are needed...

Dim result as DataTable = GetPayments(myRequest)


[blue]_______________________________________[/blue]
Business Logic:"AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle AND..." - Dr. Suess
 
aaaah,

dragonwell beat me to it.

i just did some research on this and had reached to the same opinion, the best way would be to use properties for optional parameters.

this has 2 advantages:
1. Better serialisation.
2. No overloading required.

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top