Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert VBA to C#

Convert VBA to C#

Scheduled Pinned Locked Moved C#
helpcsharpdatabasebeta-testing
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guytz
    wrote on last edited by
    #1

    Hi, I have a couple of VBA modules I need to convert to C# (Or a SQL 2005 UDF if that is a better place for it!). The VBA code is below:

    Function RMSBetaDist(x, alpha, beta, rate)
    'Main function where the cumulative Beta distribution is assembled

    datasize = Range("Datsize")
    Dim bt() As Double
    Dim RMSBeta() As Double
    Dim i As Integer

    ReDim bt(datasize)
    ReDim RMSBeta(datasize)

    For i = 1 To datasize
    If x(i, 1) < 0 Or x(i, 1) > 1 Then
    'Error = MsgBox("There is a problem with the x value of the beta function", vbOKOnly)
    'GoTo Abend
    x(i, 1) = 1
    End If
    If x(i, 1) = 0 Then
    bt(i) = 0
    RMSBeta(i) = 0
    GoTo Nextarray
    ElseIf x(i, 1) = 1 Then
    bt(i) = 0
    RMSBeta(i) = 1
    GoTo Nextarray
    Else
    bt(i) = Exp(RMSGammaln(alpha(i, 1) + beta(i, 1)) - RMSGammaln(alpha(i, 1)) - RMSGammaln(beta(i, 1)) + alpha(i, 1) * Log(x(i, 1)) + beta(i, 1) * Log(1 - x(i, 1)))
    End If
    If x(i, 1) < (alpha(i, 1) + 1) / (alpha(i, 1) + beta(i, 1) + 2) Then
    RMSBeta(i) = bt(i) * RMSBetaCF(x(i, 1), alpha(i, 1), beta(i, 1)) / alpha(i, 1)
    Else 'symmetry relation I(a,b)=1-I(b,a) where I() is the incomplete beta function
    RMSBeta(i) = 1 - bt(i) * RMSBetaCF(1 - x(i, 1), beta(i, 1), alpha(i, 1)) / beta(i, 1)
    End If

    Nextarray:
    RMSBetaDist = RMSBetaDist + rate(i) * (1 - RMSBeta(i))
    Next i

    Abend:
    End Function

    And the next one:

    Function RMSGammaln(xg) 'Lanczos gamma approximation
    'Used in the Beta distribution approximation

    Dim y, temp, serial, PI, small As Double
    Dim coeff(6) As Double
    Dim j As Integer

    PI = 3.14159265358979
    coeff(0) = 76.18009173
    coeff(1) = -86.50532033
    coeff(2) = 24.01409822
    coeff(3) = -1.231739516
    coeff(4) = 0.00120858003
    coeff(5) = -0.00000536382
    small = 0
    If xg < 1 Then
    small = xg
    y = xg - 1
    Else
    y = xg - 1
    End If
    temp = y + 5.5
    temp = temp - (y + 0.5) * Log(temp)
    serial = 1

    For j = 0 To 5
    y = y + 1
    serial = serial + coeff(j) / y
    Next j
    If small <> 0 Then 'formula for values of x < 1
    RMSGammaln = -temp + Log(Sqr(2 * PI) * serial)
    Else
    RMSGammaln = -temp + Log(Sqr(2 * PI) * serial)
    End If

    End Function

    And the last one:

    Function RMSBetaCF(x2, alpha2, beta2)
    'Used to approximate the integral portion of the cumulative Beta distribution

    Dim errorf As Double
    Dim qap, qam, qab, em, temp, d As Double
    Dim bz, bm, bp, bpp As Double
    Dim az, am, ap, app, aold As Double
    Dim m, MaxIterations As Integer

    S M 2 Replies Last reply
    0
    • G Guytz

      Hi, I have a couple of VBA modules I need to convert to C# (Or a SQL 2005 UDF if that is a better place for it!). The VBA code is below:

      Function RMSBetaDist(x, alpha, beta, rate)
      'Main function where the cumulative Beta distribution is assembled

      datasize = Range("Datsize")
      Dim bt() As Double
      Dim RMSBeta() As Double
      Dim i As Integer

      ReDim bt(datasize)
      ReDim RMSBeta(datasize)

      For i = 1 To datasize
      If x(i, 1) < 0 Or x(i, 1) > 1 Then
      'Error = MsgBox("There is a problem with the x value of the beta function", vbOKOnly)
      'GoTo Abend
      x(i, 1) = 1
      End If
      If x(i, 1) = 0 Then
      bt(i) = 0
      RMSBeta(i) = 0
      GoTo Nextarray
      ElseIf x(i, 1) = 1 Then
      bt(i) = 0
      RMSBeta(i) = 1
      GoTo Nextarray
      Else
      bt(i) = Exp(RMSGammaln(alpha(i, 1) + beta(i, 1)) - RMSGammaln(alpha(i, 1)) - RMSGammaln(beta(i, 1)) + alpha(i, 1) * Log(x(i, 1)) + beta(i, 1) * Log(1 - x(i, 1)))
      End If
      If x(i, 1) < (alpha(i, 1) + 1) / (alpha(i, 1) + beta(i, 1) + 2) Then
      RMSBeta(i) = bt(i) * RMSBetaCF(x(i, 1), alpha(i, 1), beta(i, 1)) / alpha(i, 1)
      Else 'symmetry relation I(a,b)=1-I(b,a) where I() is the incomplete beta function
      RMSBeta(i) = 1 - bt(i) * RMSBetaCF(1 - x(i, 1), beta(i, 1), alpha(i, 1)) / beta(i, 1)
      End If

      Nextarray:
      RMSBetaDist = RMSBetaDist + rate(i) * (1 - RMSBeta(i))
      Next i

      Abend:
      End Function

      And the next one:

      Function RMSGammaln(xg) 'Lanczos gamma approximation
      'Used in the Beta distribution approximation

      Dim y, temp, serial, PI, small As Double
      Dim coeff(6) As Double
      Dim j As Integer

      PI = 3.14159265358979
      coeff(0) = 76.18009173
      coeff(1) = -86.50532033
      coeff(2) = 24.01409822
      coeff(3) = -1.231739516
      coeff(4) = 0.00120858003
      coeff(5) = -0.00000536382
      small = 0
      If xg < 1 Then
      small = xg
      y = xg - 1
      Else
      y = xg - 1
      End If
      temp = y + 5.5
      temp = temp - (y + 0.5) * Log(temp)
      serial = 1

      For j = 0 To 5
      y = y + 1
      serial = serial + coeff(j) / y
      Next j
      If small <> 0 Then 'formula for values of x < 1
      RMSGammaln = -temp + Log(Sqr(2 * PI) * serial)
      Else
      RMSGammaln = -temp + Log(Sqr(2 * PI) * serial)
      End If

      End Function

      And the last one:

      Function RMSBetaCF(x2, alpha2, beta2)
      'Used to approximate the integral portion of the cumulative Beta distribution

      Dim errorf As Double
      Dim qap, qam, qab, em, temp, d As Double
      Dim bz, bm, bp, bpp As Double
      Dim az, am, ap, app, aold As Double
      Dim m, MaxIterations As Integer

      M Offline
      M Offline
      mikone
      wrote on last edited by
      #2

      hi, just start reading some c# tutorials - you'll be able to convert it very soon but dont expect anyone in here to do the work for you ;D there are several free ebooks available and you also have access to a lot of articles here on codeproject. Good luck, mik

      G 1 Reply Last reply
      0
      • G Guytz

        Hi, I have a couple of VBA modules I need to convert to C# (Or a SQL 2005 UDF if that is a better place for it!). The VBA code is below:

        Function RMSBetaDist(x, alpha, beta, rate)
        'Main function where the cumulative Beta distribution is assembled

        datasize = Range("Datsize")
        Dim bt() As Double
        Dim RMSBeta() As Double
        Dim i As Integer

        ReDim bt(datasize)
        ReDim RMSBeta(datasize)

        For i = 1 To datasize
        If x(i, 1) < 0 Or x(i, 1) > 1 Then
        'Error = MsgBox("There is a problem with the x value of the beta function", vbOKOnly)
        'GoTo Abend
        x(i, 1) = 1
        End If
        If x(i, 1) = 0 Then
        bt(i) = 0
        RMSBeta(i) = 0
        GoTo Nextarray
        ElseIf x(i, 1) = 1 Then
        bt(i) = 0
        RMSBeta(i) = 1
        GoTo Nextarray
        Else
        bt(i) = Exp(RMSGammaln(alpha(i, 1) + beta(i, 1)) - RMSGammaln(alpha(i, 1)) - RMSGammaln(beta(i, 1)) + alpha(i, 1) * Log(x(i, 1)) + beta(i, 1) * Log(1 - x(i, 1)))
        End If
        If x(i, 1) < (alpha(i, 1) + 1) / (alpha(i, 1) + beta(i, 1) + 2) Then
        RMSBeta(i) = bt(i) * RMSBetaCF(x(i, 1), alpha(i, 1), beta(i, 1)) / alpha(i, 1)
        Else 'symmetry relation I(a,b)=1-I(b,a) where I() is the incomplete beta function
        RMSBeta(i) = 1 - bt(i) * RMSBetaCF(1 - x(i, 1), beta(i, 1), alpha(i, 1)) / beta(i, 1)
        End If

        Nextarray:
        RMSBetaDist = RMSBetaDist + rate(i) * (1 - RMSBeta(i))
        Next i

        Abend:
        End Function

        And the next one:

        Function RMSGammaln(xg) 'Lanczos gamma approximation
        'Used in the Beta distribution approximation

        Dim y, temp, serial, PI, small As Double
        Dim coeff(6) As Double
        Dim j As Integer

        PI = 3.14159265358979
        coeff(0) = 76.18009173
        coeff(1) = -86.50532033
        coeff(2) = 24.01409822
        coeff(3) = -1.231739516
        coeff(4) = 0.00120858003
        coeff(5) = -0.00000536382
        small = 0
        If xg < 1 Then
        small = xg
        y = xg - 1
        Else
        y = xg - 1
        End If
        temp = y + 5.5
        temp = temp - (y + 0.5) * Log(temp)
        serial = 1

        For j = 0 To 5
        y = y + 1
        serial = serial + coeff(j) / y
        Next j
        If small <> 0 Then 'formula for values of x < 1
        RMSGammaln = -temp + Log(Sqr(2 * PI) * serial)
        Else
        RMSGammaln = -temp + Log(Sqr(2 * PI) * serial)
        End If

        End Function

        And the last one:

        Function RMSBetaCF(x2, alpha2, beta2)
        'Used to approximate the integral portion of the cumulative Beta distribution

        Dim errorf As Double
        Dim qap, qam, qab, em, temp, d As Double
        Dim bz, bm, bp, bpp As Double
        Dim az, am, ap, app, aold As Double
        Dim m, MaxIterations As Integer

        S Offline
        S Offline
        shabonaa
        wrote on last edited by
        #3

        try to use this site it converts form C# to VB.Net and vise versa. http://wwww.developerfusion.com/utilities/convertcsharptovb.aspx[^]

        D G 2 Replies Last reply
        0
        • S shabonaa

          try to use this site it converts form C# to VB.Net and vise versa. http://wwww.developerfusion.com/utilities/convertcsharptovb.aspx[^]

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          VBA is based on VB6 not VB.net. In general you'll have to do a manual port.

          S 1 Reply Last reply
          0
          • M mikone

            hi, just start reading some c# tutorials - you'll be able to convert it very soon but dont expect anyone in here to do the work for you ;D there are several free ebooks available and you also have access to a lot of articles here on codeproject. Good luck, mik

            G Offline
            G Offline
            Guytz
            wrote on last edited by
            #5

            Thanks Mik, Am doing a C# course at the moment...not so much the C# that is the problem, more the Maths and VBA and knowing where to start! Cheers, Guytz

            1 Reply Last reply
            0
            • S shabonaa

              try to use this site it converts form C# to VB.Net and vise versa. http://wwww.developerfusion.com/utilities/convertcsharptovb.aspx[^]

              G Offline
              G Offline
              Guytz
              wrote on last edited by
              #6

              Thanks shabonaa, This has given me a good place to start. Thanks, Guytz

              1 Reply Last reply
              0
              • D Dan Neely

                VBA is based on VB6 not VB.net. In general you'll have to do a manual port.

                S Offline
                S Offline
                shabonaa
                wrote on last edited by
                #7

                of course my dear but it will give a bit help

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups