-
May 21st, 2018, 06:37 AM
#1
Thread Starter
New Member
Help with Template
Hello friends,
I'm trying to transform the following function to a template, returning different types of integers (UInteger, ULong, Decimal...)
Code:
Public Shared Function BinomialCoefficient(n As Integer, k As Integer) As Integer
If (k < 0) OrElse (k > n) Then Return 0
If (k = 0) OrElse (k = n) Then Return 1
k = Math.Min(k, n - k)
Try
Dim C(k + 1) As Integer : C(0) = 1
For i As Integer = 1 To n
For j As Integer = Math.Min(i, k) To 1 Step -1
C(j) = C(j) + C(j - 1)
Next
Next
Return C(k)
Catch ex As Exception
Throw New OverflowException()
End Try
End Function
Code:
Public Shared Function BinomialCoefficient(Of T)(n As Integer, k As Integer) As T
If (k < 0) OrElse (k > n) Then Return 0
If (k = 0) OrElse (k = n) Then Return 1
k = Math.Min(k, n - k)
Try
Dim C(k + 1) As T : C(0) = 1
For i As Integer = 1 To n
For j As Integer = Math.Min(i, k) To 1 Step -1
C(j) = C(j) + C(j - 1)
Next
Next
Return C(k)
Catch ex As Exception
Throw New OverflowException()
End Try
End Function
Is there any way to solve this problem instead of coding N different functions with different signature names?
Any help/suggestion will be appreciated.
-
May 21st, 2018, 07:50 AM
#2
Re: Help with Template
For the record, Decimal is not a type of integer, but I imagine that that was just an oversight. Unfortunately, I don't think that there is a way you could do it using generics because there's no single type that you can use a constraint that would allow you to perform arithmetic operations on type T. You could use Reflection but that seems to be a bit of a hack. I'd probably just go with the separate methods.
-
May 22nd, 2018, 05:32 PM
#3
Thread Starter
New Member
Re: Help with Template
Originally Posted by jmcilhinney
... I'd probably just go with the separate methods.
Yes, that was what I did, making the code more readable.
Code:
Public Class Binomial
Public Shared Function [Integer](n As Integer, k As Integer) As Integer
Public Shared Function [ULong](n As Integer, k As Integer) As ULong
Public Shared Function [Decimal](n As Integer, k As Integer) As Decimal
End Class
jmcilhinney, another silly question: how can I add a linebreak inside the xml <summary> ?
Code:
''' <summary>
'''
''' linebreak here <--
'''
''' </summary>
''' <param name="n"></param>
''' <param name="k"></param>
''' <returns></returns>
Public Shared Function [Integer](n As Integer, k As Integer) As Integer
Thank you my friend!
-
May 22nd, 2018, 06:11 PM
#4
Re: Help with Template
Originally Posted by Coding4Fun
jmcilhinney, another silly question: how can I add a linebreak inside the xml <summary> ?
Code:
''' <summary>
'''
''' linebreak here <--
'''
''' </summary>
''' <param name="n"></param>
''' <param name="k"></param>
''' <returns></returns>
Public Shared Function [Integer](n As Integer, k As Integer) As Integer
Thank you my friend!
For future reference, you should not ask unrelated questions in the same thread. Even related questions should generally be separated into their own threads, unless each represents a relatively small and/or closely-related topic.
https://docs.microsoft.com/en-us/dot...ation-comments
There is nothing for a line break specifically but you can use the <para> tag to create paragraphs and thus break between them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|