Vb.Net Value show zero for next loop ctype - vb.net

After run the program I show value of Power1 is zero, why?
Here codes;
Dim Guc1....Guc59 as decimal
Guc1=Val(TextBox5.Text)*Val(Textbox98.Text)
.
.
.
Guc59=Val(TextBox42.Text)*Val(Textbox12.Text)
For i = 1 To 59 Step 2
Dim txt As TextBox = CType(TabControlPanel1.Controls("TextBoxX" & i), TextBox)
Dim guc As Decimal = CType(("Guc".ToString & i), Decimal)
Hız1 = Val(txt.Text) * RollinRadius * 3.14 * 3.6 / (Val(TextBox1.Text) * Val(TextBox33.Text) * 30)
Power1 = guc * 3.14 / (30 * 1000) *2 *3)
ListBox2.Items.Add(Power1)
Next

Power1 is zero because guc will currently always be zero. You cannot access variables by their names like you're trying to do. "Guc" & i will just result in a normal string, which it cannot convert since Guc is not numerical. The only way to access variables dynamically by their name is to use Reflection.
But the best way to do what you're trying to achieve is to use a List(Of T) or an Array:
Dim Guc As Decimal = New Decimal(59 - 1) {} '59 - 1 = 59 items.
...
Dim gucx As Decimal = Guc(i - 1)
...
Power1 = gucx * 3.14 / (30 * 1000) * 2 * 3

Related

I need to multiply 1st Array list with 2nd Array list

In here get value into the arraylist, I'm using MySql database and Tables.
my 1st array list number of items can be 1 or 2 or 3 or 4 or 5,
my 2nd array contain number of 5 items.
for example;
arraylist1 = (10,50,50)
arraylist2 = (2.6,2.4,1.7,1.4,1.1)
If when multiplying arraylist together,
total = (10 * 2.6) + (50 * 2.4) + (50 * 1.7) places can't be changed.
How to do that?
firstly I try with define the arralist2 and it worked.
arraylist2 = (2.6,2.4,1.7,1.4,1.1)
Code;
Dim a As Integer = 0
For Each i In arraylist1
Ptotal += arraylist2(a) * i
a += 1
Next
Dim a As Integer = 0
For Each i In distance_range
Ptotal += price_rage(a) * i
a += 1
Next

Calculating distance in kilometers between coordinates

I'm trying to calculate distance in kilometers between two geographical coordinates using the haversine formula.
Code:
Dim dbl_dLat As Double
Dim dbl_dLon As Double
Dim dbl_a As Double
dbl_P = WorksheetFunction.Pi / 180
dbl_dLat = dbl_P * (dbl_Latitude2 - dbl_Latitude1)
dbl_dLon = dbl_P * (dbl_Longitude2 - dbl_Longitude1)
dbl_a = Sin(dbl_dLat / 2) * Sin(dbl_dLat / 2) + Cos(dbl_Latitude1 * dbl_P) * Cos(dbl_Latitude2 * dbl_P) * Sin(dbl_dLon / 2) * Sin(dbl_dLon / 2)
dbl_Distance_KM = 6371 * 2 * WorksheetFunction.Atan2(Sqr(dbl_a), Sqr(1 - dbl_a))
I'm testing with these coordinates:
dbl_Longitude1 = 55.629178
dbl_Longitude2 = 29.846686
dbl_Latitude1 = 37.659466
dbl_Latitude2 = 30.24441
And the code returns 20015.09, which is obviously wrong. It should be 642 km according to Yandex maps.
Where am I wrong? Are the longitude and latitude in wrong format?
As far as I can tell, the issue is that the order of arguments to atan2() varies by language. The following works* for me:
Option Explicit
Public Sub Distance()
Dim dbl_Longitude1 As Double, dbl_Longitude2 As Double, dbl_Latitude1 As Double, dbl_Latitude2 As Double
dbl_Longitude1 = 55.629178
dbl_Longitude2 = 29.846686
dbl_Latitude1 = 37.659466
dbl_Latitude2 = 30.24441
Dim dbl_dLat As Double
Dim dbl_dLon As Double
Dim dbl_a As Double
Dim dbl_P As Double
dbl_P = WorksheetFunction.Pi / 180
dbl_dLat = dbl_P * (dbl_Latitude2 - dbl_Latitude1) 'to radians
dbl_dLon = dbl_P * (dbl_Longitude2 - dbl_Longitude1) 'to radians
dbl_a = Sin(dbl_dLat / 2) * Sin(dbl_dLat / 2) + _
Cos(dbl_Latitude1 * dbl_P) * Cos(dbl_Latitude2 * dbl_P) * Sin(dbl_dLon / 2) * Sin(dbl_dLon / 2)
Dim c As Double
Dim dbl_Distance_KM As Double
c = 2 * WorksheetFunction.Atan2(Sqr(1 - dbl_a), Sqr(dbl_a)) ' *** swapped arguments to Atan2
dbl_Distance_KM = 6371 * c
Debug.Print dbl_Distance_KM
End Sub
*Output: 2507.26205401321, although gcmap.com says the answer is 2512 km. This might be a precision issue --- I think it's close enough to count as working. (Edit it might also be that gcmap uses local earth radii rather than the mean radius; I am not sure.)
Explanation
I found this description of the haversine formula for great-circle distance, which is what you are implementing. The JavaScript implementation on that page gives this computation for c:
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
In JavaScript, atan2() takes parameters y, x. However, in Excel VBA, WorksheetFunction.Atan2 takes parameters x, y. Your original code passed Sqr(dbl_a) as the first parameter, as it would be in JavaScript. However, Sqr(dbl_a) needs to be the second parameter in Excel VBA.
A comment on naming
Building on #JohnColeman's point, there are lots of ways to name variables. In this case, I would recommend using the prefixes for unit rather than for type: e.g., deg_Latitude1, RadPerDeg = Pi/180, and rad_dLat = RadPerDeg * (deg_Latitude2 - deg_Latitude1). I personally think that helps avoid unit-conversion mishaps.
My VBA code that returns the answer in feet; However 'd' is the answer in kilometers.
Imports System.Math
Module Haversine
Public Function GlobalAddressDistance(sLat1 As String, sLon1 As String, sLat2 As String, sLon2 As String) As String
Const R As Integer = 6371
Const cMetersToFeet As Single = 3.2808399
Const cKiloMetersToMeters As Integer = 1000
Dim a As Double = 0, c As Double = 0, d As Double = 0
'Convert strings to numberic double values
Dim dLat1 As Double = Val(sLat1)
Dim dLat2 As Double = Val(sLat2)
Dim dLatDiff As Double = DegreesToRadians(CDbl(sLat2) - CDbl(sLat1))
Dim dLonDiff As Double = DegreesToRadians(CDbl(sLon2) - CDbl(sLon1))
a = Pow(Sin(dLatDiff / 2), 2) + Cos(DegreesToRadians(dLat1)) * Cos(DegreesToRadians(dLat2)) * Pow(Sin(dLonDiff / 2), 2)
c = 2 * Atan2(Sqrt(a), Sqrt(1 - a))
d = R * c
'Convert kilometers to feet
Return Format((d * cKiloMetersToMeters * cMetersToFeet), "0.##").ToString
End Function
Private Function DegreesToRadians(ByVal dDegrees As Double) As Double
Return (dDegrees * PI) / 180
End Function
End Module

MS Excel. VBA function returns #value

It would be nice if someone could explain what causes function above return #value error.
Public Function papild(x)
Dim Sum As Double, A As Double, pi As Double,
Sum = 0.5 - (x - pi / 4)
A = -(x - pi / 4)
pi = Application.WorksheetFunction.pi()
Dim k As Integer, i As Integer
k = 2
i = 0
Do While Abs(A) > 0.0001
A = -A * 4 * A * A / (k + i) * (k + i + 1)
Sum = Sum + A
k = k + 1
i = i + 1
Loop
paplid = Sum
End Function
Function takes x value from MS Excel cell and it's equal = -1.5708 (=-PI()/2 #Formula Bar)
In lines 3 and 4 you work with variable pi before setting it in line 5...
Could there be some brackets missing in your formula. It basically says:
A = -4A^3 * (k+i+1)/(k+1)
This obviously drifts to +/- infinite so your loop cannot end.
Also there is a comma too much in the second line and a spelling error in the last line (paplid instead of papild).
Have you tried debugging the code?
When I run the code I get an overflow error # the 6th iteration of the while loop starting with x = -1.5708. Number gets to large to fit inside variable
.Other than that there are some minor things:
missing As Double
Public Function papild(x) As Double
and unnecessary comma at the end
Dim Sum As Double, A As Double, pi As Double,

Transform decimal is removing my zeros

how can I make sure that the results dont remove the last 0 for example, this produces "1.454" instead of "1.4540"
Dim test1 As Decimal = 14540
Debug.Print(TransformDecimal(test1, 1))
Private Shared Function TransformDecimal(value As Decimal, numberOfPlaces As Integer) As Decimal
Dim min = CDec(Math.Pow(10, numberOfPlaces - 1))
Dim max = CDec(Math.Pow(10, numberOfPlaces))
If (value >= max) Then
While value >= max
value /= 10
End While
ElseIf (value < min) Then
While value < min
value *= 10
End While
End If
Return value
End Function
You really must put Option Explicit On.
When you don't and you have, for example, this code Dim x As Decimal = 1.0 you are creating a Double and then converting it to Decimal. With Option Strict On this code won't compile.
When you write this code Dim x As Decimal = 1.0d you are immediately creating a Decimal- and that preserves the decimal places.
So, if you now write your method like this:
Private Shared Function TransformDecimal(value As Decimal, numberOfPlaces As Integer) As Decimal
Dim min = CDec(Math.Pow(10, numberOfPlaces - 1))
Dim max = CDec(Math.Pow(10, numberOfPlaces))
If (value >= max) Then
While value >= max
Dim bits = Decimal.GetBits(value)
bits(3) = ((bits(3) \ 65536) + 1) * 65536
value = New Decimal(bits)
End While
ElseIf (value < min) Then
While value < min
Dim bits = Decimal.GetBits(value)
bits(3) = ((bits(3) \ 65536) - 1) * 65536
value = New Decimal(bits)
End While
End If
Return value
End Function
And call it like this:
Dim test1 As Decimal = 14540d
Debug.Print(TransformDecimal(test1, 1).ToString())
You'll find that you get the result you want:
1.4540
The crux of this code is the lines bits(3) = ((bits(3) \ 65536) + 1) * 65536 & bits(3) = ((bits(3) \ 65536) - 1) * 65536 which shift the exponent to change the decimal by multiples of 10.

Getting NaN for a double datatype

I am trying to run some mathematical formula which has large values, so I am using the Double datatype. But still I am getting the answer as NaN if I input large values. So how can I solve this?
For example, if I pass Varx=3 and countx=1230 I get the result as NaN.
Public Function EulerForPro(ByVal Varx As Integer, ByVal Countx As Integer) As Double
Dim Result1 As Double = 1
Dim Result2 As Double = Varx
Dim Result As Double = 0
For i = 1 To Countx
Result1 = Result1 + (Math.Pow(-1, i) * Math.Pow(Varx, (2 * i)) / factx(2 * i))
Next
For i = 1 To Countx
Result2 = Result2 + (Math.Pow(-1, i) * Math.Pow(Varx, ((2 * i) + 1)) / factx((2 * i) + 1))
Next
Result = Result1 + Result2
Label2.Text = Result1
Label3.Text = Result2
Label4.Text = Result
End Function
Use the logarithm of the results. It is standard practice for large numbers.
So instead of multiplying a*b (which causes overflow), use
Y = log(a) + log(b)
You could try using the Decimal data type instead. It can hold an integer value of approximately 7,9 x 10^28 (the exact number is 2^96 - 1, since it can use 96 bits of its 128 for storing the integer part of a value). However, it's a complex data type and you should carefully consider how to use it in your code. Not all implicit conversions work, especially when using Double.
See MSDN on decimal for exact specifications of Decimal.