Results 1 to 4 of 4

Thread: Help with Template

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    11

    Re: Help with Template

    Quote Originally Posted by jmcilhinney View Post
    ... 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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Help with Template

    Quote Originally Posted by Coding4Fun View Post
    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
  •  



Click Here to Expand Forum to Full Width