Answered by:
convert vb code into c# code

Question
-
Public Function Power(x As Double, E As Double) As Double
Select Case E
Case 2
Power = x * x
Case 3
Power = x * x * x
Case 4
Power = x * x * x * x
Case 5
Power = x * x * x * x * x
Case 6
Power = x * x * x * x * x * x
Case 7
Power = x * x * x * x * x * x * x
Case 0.5
Power = Sqr(x)
End Select
End FunctionFriday, December 27, 2013 6:37 PM
Answers
-
Hi Deep12,
What you have looks like VB6 code, not VB.Net. You might be able to find a VB6 to VB.Net converter to help you with that, but you should be able to read the code and convert it by hand. The folks in the primary VB forum may also be able to help. If you load it into a VB.Net project then Visual Studio will flag the errors and make suggestions.
Most of the issues are repeats of the same thing. For example, the structure:
Public Type NZTM Northing As Double Easting As Double End Type
coverts to the following VB.Net code:
Public Structure NZTM Dim Northing As Double Dim Easting As Double End Structure
Visual Studio will automatically flag the Type to Structure change and offer to perform the conversion for you. It will then tell you that a "Declaration is expected" for the fields, which you fix by adding the "Dim" keyword.
The equivalent C# structure will be:
public struct NZTM { public double Northing; public double Easting; }
That will get you close. You can change the Debug.Print calls to Debug.WriteLine and use System.Math functions for the Sin, Cos, Pow, etc.
There are also a few variables which are either typos or undeclared so you'll need to fix those up.
--Rob
- Marked as answer by Anne Jing Monday, January 6, 2014 9:46 AM
Friday, December 27, 2013 7:55 PMModerator
All replies
-
You can use Math.Pow(x,y) in both VB and C# rather than implementing this yourself.
--Rob
Friday, December 27, 2013 6:44 PMModerator -
Rob's suggestion is a good idea. In case you still want to convert to C#, you can use this online converter.
- Brady My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!
Friday, December 27, 2013 6:47 PM -
I am new to this, i actually have a vb file which needs to be converted into c#. I tried using some online converter but it gave errors. With difficulties i am manually converting now. It will be a great help if u correct the errors what the vb file has, so that i can convert it with some tool and use it. Following is the Vb code i have. Thank u.
Attribute VB_Name = "Module1"
Public Northing As Double, Easting As Double
Public rad2deg As Double, TwoPI As Double, deg2rad As Double
Public a As Double, f As Double, k0 As Double, lat0 As Double, lamba0 As Double, Efalse As Double, E0 As Double, E1 As Double, n0 As Double, B As Double, E2 As Double, E4 As Double, E6 As Double, A0 As Double, A2 As Double, A4 As Double, A6 As Double, Pi As Double
Public Type NZTM
Northing As Double
Easting As Double
End Type
Public Type NZMG
Northing As Double
Easting As Double
End Type
Public Type LatLong
Latitude As Double
Longitude As Double
End Type
Public Type ComplexNumb
Real As Double
Imaginary As Double
End Type
Public Type ICPRec
LDate As Date
Locality As String
ICP As String
Northing As Double
Easting As Double
ONorthing As Double
OEasting As Double
End Type
Public Sub Main()
Pi = 3.14159265358979
rad2deg = 180 / Pi
deg2rad = Pi / 180
TwoPI = 2 * Pi
a = 6378137
f = 1 / 298.257222101
k0 = 0.9996
lat0 = 0 / rad2deg
lambda0 = 173 / rad2deg
n0 = 10000000
Efalse = 1600000
B = a * (1 - f)
E2 = (2 * f) - Power(f, 2)
E4 = E2 * E2
E6 = E4 * E2
A0 = 1 - (E2 / 4) - (3 * E4 / 64) - (5 * E6 / 256)
A2 = 3 * (E2 + E4 / 4 + 15 * E6 / 128) / 8
A4 = 15 * (E4 + 3 * E6 / 4) / 256
A6 = 35 * E6 / 3072
End Sub
Public Function NZTMToLatLong(n As Double, East As Double) As LatLong
Dim m As Double, n1 As Double, G As Double, delta As Double, phi As Double, rho As Double, nu As Double, psi As Double, t As Double, x As Double, t1 As Double, t2 As Double, t3 As Double, t4 As Double, z1 As Double, z2 As Double, z3 As Double, z4 As Double, z5 As Double
n1 = n - n0
Debug.Print "n1 = " & n1
m0 = MeridianArc(lat0)
Debug.Print "m0 = " & m0
m = m0 + (n1 / k0)
Debug.Print "m = " & m
n = (a - B) / (a + B)
Debug.Print "n = " & n
G = a * (1 - n) * (1 - Power(n, 2)) * (1 + ((9 / 4) * Power(n, 2)) + ((255 / 64) * Power(n, 4))) * (Pi / 180)
Debug.Print "G = " & G
delta = (m / G) * deg2rad
Debug.Print "delta = " & delta
z1 = ((1.5 * n) - (27 / 32) * Power(n, 3)) * Sin(2 * delta)
z2 = (((21 / 16) * Power(n, 2)) - ((55 / 32) * Power(n, 4))) * Sin(4 * delta)
z3 = ((151 / 96) * Power(n, 3)) * Sin(6 * delta)
z4 = ((1097 / 512) * Power(n, 4)) * Sin(8 * delta)
phi = delta + z1 + z2 + z3 + z4
Debug.Print "phi = " & phi
z1 = 1 - (E2 * Power(Sin(phi), 2))
rho = (a * (1 - E2)) / (Power(Power(z1, 3), 0.5))
Debug.Print "rho = " & rho
nu = a / Power((1 - (E2 * Power(Sin(phi), 2))), 0.5)
Debug.Print "nu = " & nu
psi = nu / rho
Debug.Print "psi = " & psi
t = Tan(phi)
Debug.Print "t = " & t
E = East - Efalse
Debug.Print "E = " & E
x = E / (k0 * nu)
Debug.Print "chi or x = " & x
t1 = t * E * x / (k0 * rho * 2)
Debug.Print "t1 = " & t1
t2 = t * E * Power(x, 3) / (k0 * rho * 24) * (-4 * Power(psi, 2) + 9 * psi * (1 - Power(t, 2)) + 12 * Power(t, 2))
Debug.Print "t2 = " & t2
t3 = t * E * Power(x, 5) / (k0 * rho * 720) * (8 * Power(psi, 4) * (11 - 24 * Power(t, 2)) - 12 * Power(psi, 3) * (21 - 71 * Power(t, 2)) + 15 * Power(psi, 2) * (15 - 98 * Power(t, 2) + 15 * Power(t, 4)) + 180 * psi * (5 * Power(t, 2) - 3 * Power(psi, 4)) + (360 * Power(psi, 4)))
Debug.Print "t3 = " & t3
t4 = ((t * E * Power(x, 7)) / (k0 * rho * 40320)) * ((1385 - (3633 * Power(t, 2)) + (4095 * Power(t, 4)) + 1575 * Power(t, 6)))
Debug.Print "t4 = " & t4
NZTMToLatLong.Latitude = rad2deg * (phi - t1 + t2 - t3 + t4)
Debug.Print "Latitude = " & NZTMToLatLong.Latitude
Debug.Print "'"
t1 = x * (1 / Cos(phi))
Debug.Print "t1 = " & t1
t2 = (Power(x, 3) * (1 / Cos(phi) / 6)) * (psi + (2 * Power(t, 2)))
Debug.Print "t2 = " & t2
z1 = Power(x, 5) / (Cos(phi) * 120)
z2 = (-4 * Power(phi, 3) * (1 - (6 * Power(t, 2))))
z3 = Power(phi, 2) * (9 - (68 * Power(t, 2)))
z4 = 72 * phi * Power(t, 2)
t3 = z1 * (z2 + z3 + z4 + (24 * Power(t, 4)))
Debug.Print "t3 = " & t3
t4 = Power(x, 7) / (5040 * Cos(phi)) * (61 + 662 * Power(t, 2) + 1320 * Power(t, 4) + 720 * Power(t, 6))
Debug.Print "t4 = " & t4
NZTMToLatLong.Longitude = rad2deg * ((173 / rad2deg) + t1 - t2 + t3 - t4)
Debug.Print "Longitude = " & NZTMToLatLong.Longitude
End Function
Public Function AddC(a As ComplexNumb, B As ComplexNumb) As ComplexNumb
On Error Resume Next
AddC.Real = a.Real + B.Real
AddC.Imaginary = a.Imaginary + B.Imaginary
End Function
Public Function TimesInt(a As ComplexNumb, B As Integer) As ComplexNumb
On Error Resume Next
TimesInt.Real = a.Real * B
TimesInt.Imaginary = a.Imaginary * B
End Function
Public Function TimesDbl(a As ComplexNumb, B As Double) As ComplexNumb
On Error Resume Next
TimesDbl.Real = a.Real * B
TimesDbl.Imaginary = a.Imaginary * B
End Function
Public Function DivC(a As ComplexNumb, B As ComplexNumb) As ComplexNumb
Dim cB As ComplexNumb
DivC.Real = ((a.Real * B.Real) + (a.Imaginary * B.Imaginary)) / ((B.Real ^ 2) + (B.Imaginary ^ 2))
DivC.Imaginary = ((a.Imaginary * B.Real) - (a.Real * B.Imaginary)) / ((B.Real ^ 2) + (B.Imaginary ^ 2))
End Function
Public Function MultC(a As ComplexNumb, B As ComplexNumb) As ComplexNumb
On Error Resume Next
MultC.Real = (a.Real * B.Real) - (a.Imaginary * B.Imaginary)
MultC.Imaginary = (a.Real * B.Imaginary) + (a.Imaginary * B.Real)
End Function
Public Function PowerC(a As ComplexNumb, B As Double) As ComplexNumb
Dim x As Integer, Tmp As ComplexNumb
On Error Resume Next
If B = 0 Then PowerC.Real = 1: PowerC.Imaginary = 0: Exit Function
If B = 0 Then
PowerC.Real = 0
PowerC.Imaginary = 0
Exit Function
End If
If B = 1 Then
PowerC.Real = a.Real
PowerC.Imaginary = a.Imaginary
Exit Function
End If
Tmp.Real = a.Real
Tmp.Imaginary = a.Imaginary
For x = 2 To B
Tmp = MultC(Tmp, a)
Next
PowerC.Real = Tmp.Real
PowerC.Imaginary = Tmp.Imaginary
End Function
Public Function Power(x As Double, E As Double) As Double
Select Case E
Case 2
Power = x * x
Case 3
Power = x * x * x
Case 4
Power = x * x * x * x
Case 5
Power = x * x * x * x * x
Case 6
Power = x * x * x * x * x * x
Case 7
Power = x * x * x * x * x * x * x
Case 0.5
Power = Sqr(x)
End Select
End Function
' parameter is radians
Public Function MeridianArc(Lat As Double) As Double
'Lat /= rad2deg;
MeridianArc = a * (A0 * Lat - A2 * Math.Sin(2 * Lat) + A4 * Math.Sin(4 * Lat) - A6 * Math.Sin(6 * Lat))
End Function
' parameters are decimal degrees
Public Function ToNZTM(Longitude As Double, Latitude As Double) As NZTM
On Error Resume Next
' convert to radians
Dim sin_lat As Double, cos_lat As Double, cos_lat2 As Double, cos_lat3 As Double, cos_lat4 As Double, cos_lat5 As Double, cos_lat6 As Double, cos_lat7 As Double, lambda0 As Double
Dim rho As Double, eta As Double, psi As Double, psi2 As Double, psi3 As Double, psi4 As Double, tau As Double, tau2 As Double, tau4 As Double, tau6 As Double, tan2 As Double, omega As Double
Dim omega2 As Double, omega4 As Double, omega6 As Double, omega8 As Double, NTerm1 As Double, NTerm2 As Double, NTerm3 As Double, NTerm4 As Double, ETerm1 As Double, ETerm2 As Double, ETerm3 As Double, Eterm5 As Double, m0 As Double
E0 = 1600000
Latitude = Latitude / rad2deg
Longitude = Longitude / rad2deg
lambda0 = 173 / rad2deg
sin_lat = Sin(Latitude)
cos_lat = Cos(Latitude)
cos_lat2 = cos_lat * cos_lat
cos_lat3 = cos_lat2 * cos_lat
cos_lat4 = cos_lat3 * cos_lat
cos_lat5 = cos_lat4 * cos_lat
cos_lat6 = cos_lat5 * cos_lat
cos_lat7 = cos_lat6 * cos_lat
rho = a * (1 - E2) / ((1 - E2 * sin_lat * sin_lat) ^ 1.5)
eta = a / (1 - E2 * sin_lat * sin_lat) ^ 0.5
psi = eta / rho
psi2 = psi * psi
psi3 = psi2 * psi
psi4 = psi2 * psi2
tau = Tan(Latitude)
tau2 = tau * tau
tau4 = tau2 * tau2
tau6 = tau4 * tau2
omega = Longitude - lambda0
While omega > Pi
omega = omega - TwoPI
Wend
While omega < -Pi
omega = omega + TwoPI
Wend
omega2 = omega * omega
omega4 = omega2 * omega2
omega6 = omega4 * omega2
omega8 = omega6 * omega2
NTerm1 = omega2 * eta * sin_lat * cos_lat / 2
NTerm2 = omega4 * eta * sin_lat * cos_lat3 * (4 * psi2 + psi - tau2) / 24
NTerm3 = omega6 * eta * sin_lat * cos_lat5 * (8 * psi4 * (11 - 24 * tau2) - 28 * psi3 * (1 - 6 * tau2) + psi2 * (1 - 32 * tau2) - psi * 2 * tau2 + tau4) / 720
NTerm4 = omega8 * eta * sin_lat * cos_lat7 * (1385 - 3111 * tau2 + 543 * tau4 - tau6) / 40320
ETerm1 = omega2 * cos_lat2 * (psi - tau2) / 6#
ETerm2 = omega4 * cos_lat4 * (4# * psi3 * (1# - 6# * tau2) + psi2 * (1# + 8# * tau2) - psi * 2# * tau2 + tau4) / 120#
ETerm3 = omega6 * cos_lat6 * (61# - 479# * tau2 + 179# * tau4 - tau6) / 5040#
ToNZTM.Northing = n0 + k0 * (MeridianArc(Latitude) - m0 + NTerm1 + NTerm2 + NTerm3 + NTerm4)
ToNZTM.Easting = E0 + k0 * eta * omega * cos_lat * (1 + ETerm1 + ETerm2 + ETerm3)
End Function
Public Function TOLatLong(Northing As Double, Easting As Double) As LatLong
Dim Thetai As ComplexNumb, Thetaiplus1 As ComplexNumb, Z As ComplexNumb, x As Double
Dim Term1 As ComplexNumb, Term2 As ComplexNumb, n As Double, I As Integer
Dim DeltaPhi As Double, DeltaLamba As Double, D(8) As Double
Dim NE As ComplexNumb, B(6) As ComplexNumb, AC As ComplexNumb, q As ComplexNumb
Dim C(6) As ComplexNumb, Phi0 As Double, a As Double, n0 As Double, lambda As Double
DeltaPhi = 0
a = 6378388
'n0 = 6023155
n0 = 6022952
'E0 = 2510000
E0 = 2509977
Phi0 = -41
lambda = 173
B(0).Real = 0.7557853228
B(0).Imaginary = 0
B(1).Real = 0.249204646
B(1).Imaginary = 0.003371507
B(2).Real = -0.001541739
B(2).Imaginary = 0.04105856
B(3).Real = -0.10162907
B(3).Imaginary = 0.01727609
B(4).Real = -0.26623489
B(4).Imaginary = -0.36249218
B(5).Real = -0.6870983
B(5).Imaginary = -1.1651967
C(0).Real = 1.3231270439
C(0).Imaginary = 0
C(1).Real = -0.577245789
C(1).Imaginary = -0.007809598
C(2).Real = 0.508307513
C(2).Imaginary = -0.112208952
C(3).Real = -0.15094762
C(3).Imaginary = 0.18200602
C(4).Real = 1.01418179
C(4).Imaginary = 1.64497696
C(5).Real = 1.9660549
C(5).Imaginary = 2.5127645
AC.Real = 6378388
AC.Imaginary = 0
Const A1 = 0.6399175073
Const A2 = -0.1358797613
Const A3 = 0.063294409
Const A4 = -0.02526853
Const A5 = 0.0117879
Const A6 = -0.0055161
Const A7 = 0.0026906
Const A8 = -0.001333
Const A9 = 0.00067
Const A10 = -0.00034
D(0) = 1.5627014243
D(1) = 0.5185406398
D(2) = -0.03333098
D(3) = -0.1052906
D(4) = -0.0368594
D(5) = 0.007317
D(6) = 0.0122
D(7) = 0.00394
D(8) = -0.0013
Z.Real = (Northing - n0) / a
Z.Imaginary = (Easting - E0) / a
For n = 0 To 5
Thetai = AddC(Thetai, MultC(PowerC(Z, n + 1), C(n)))
Next
For I = 0 To 1
Term1.Real = 0
Term1.Imaginary = 0
Term2.Real = 0
Term2.Imaginary = 0
For n = 2 To 6
Term1 = AddC(Term1, MultC(TimesDbl(B(n - 1), (n - 1)), PowerC(Thetai, n)))
Next
For n = 1 To 6
Term2 = AddC(Term2, MultC(TimesDbl(B(n - 1), n), PowerC(Thetai, n - 1)))
Next
Thetaiplus1 = DivC(AddC(Z, Term1), Term2)
Thetai = Thetaiplus1
Next
DeltaLamba = Thetaiplus1.Imaginary
For n = 0 To 8
DeltaPhi = DeltaPhi + (D(n) * (Thetaiplus1.Real ^ (n + 1)))
Next
TOLatLong.Latitude = Phi0 + (DeltaPhi * (1000 / 36))
TOLatLong.Longitude = lambda + (DeltaLamba * (180 / Pi))
End FunctionFriday, December 27, 2013 6:57 PM -
Hi Deep12,
What you have looks like VB6 code, not VB.Net. You might be able to find a VB6 to VB.Net converter to help you with that, but you should be able to read the code and convert it by hand. The folks in the primary VB forum may also be able to help. If you load it into a VB.Net project then Visual Studio will flag the errors and make suggestions.
Most of the issues are repeats of the same thing. For example, the structure:
Public Type NZTM Northing As Double Easting As Double End Type
coverts to the following VB.Net code:
Public Structure NZTM Dim Northing As Double Dim Easting As Double End Structure
Visual Studio will automatically flag the Type to Structure change and offer to perform the conversion for you. It will then tell you that a "Declaration is expected" for the fields, which you fix by adding the "Dim" keyword.
The equivalent C# structure will be:
public struct NZTM { public double Northing; public double Easting; }
That will get you close. You can change the Debug.Print calls to Debug.WriteLine and use System.Math functions for the Sin, Cos, Pow, etc.
There are also a few variables which are either typos or undeclared so you'll need to fix those up.
--Rob
- Marked as answer by Anne Jing Monday, January 6, 2014 9:46 AM
Friday, December 27, 2013 7:55 PMModerator