Functions

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

Hi,

How can I create a function that will accept as a parameter either a datarow
or a datarow array? I want to do this without creating two different
functions.

Thanks,
Nathan
 
Nathan said:
How can I create a function that will accept as a parameter either a
datarow or a datarow array? I want to do this without creating two
different functions.

You can overload the method of it has a different signature. This is the
preferred way.
 
Nathan,

That is what you call 2 functions

function myfunction (byval dr as datarow, byval field as integer) as string
return dr(field).ToString
end function
function myfunction (byval drs as datarowcollection, byval field as integer,
byval row as integer) as string
return drs(row)(field).ToString
end function

This is named one method however overloaded and you have to write 2
functions.

I hope this gives an idea.

Cor
 
You can declare the function parameter as an object then do a type check and
cast in order to use the variable within the function
 
Nathan,
In addition to the other comments, I would overload the method as Herfried
(and others) suggests.

However I would implement one of the functions in terms of the other, to
minimize duplicate code.

For example:

Public Sub DoSomething(ByVal row As DataRow)
' process a single row
End Sub

Public Sub DoSomething(ByVal rows() As DataRow)
For Each row As DataRow In rows
DoSomething(row)
Next
End Sub

Depending on which method is more often & what I was doing I may reverse the
above two: (Normally I find myself doing this when I write the array version
first, then notice a need for the single row version).

Public Sub DoSomething(ByVal row As DataRow)
DoSomething(New DataRow() { row })
End Sub

Public Sub DoSomething(ByVal rows() As DataRow)
For Each row As DataRow In rows
' process a single row
Next
End Sub

Alternatively you can use a ParamArray parameter:

Public Sub DoSomething(ParamArray ByVal rows() As DataRow)
For Each row As DataRow In rows
' process a single row
Next
End Sub

However the ParamArray version allows an Array or rows or 1 row as you want.
Plus zero and 2 or more rows which you may not want.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top