Using a variable and its value across multiple subs - vb.net

Here is my code.
Module Module1
Dim a, x, y As Integer
Public Delegate Sub Areas()
Sub Main()
Dim Square As Areas
Dim Rectangle As Areas
Dim Add As Areas
Dim Area As [Delegate]
Square = New Areas(AddressOf areasquare)
Rectangle = New Areas(AddressOf arearect)
Add = New Areas(AddressOf addition)
Area = [Delegate].Combine(Square, Rectangle, Add)
Area.DynamicInvoke()
Console.WriteLine("the combined areas are equal to " & x * y + a * a)
Console.ReadKey()
End Sub
Class SqareArea
Public Function mult(ByVal a As Integer)
Return a * a
End Function
End Class
Class RectangleArea
Public Function mult(ByVal x As Integer, ByVal y As Integer)
Return x * y
End Function
End Class
Public Sub areasquare()
Dim a As Integer
Dim objSqareArea As New SqareArea()
Console.WriteLine("What is the measurement of one side of the square?")
a = Int32.Parse(Console.ReadLine)
Console.WriteLine("The area of the square is " & objSqareArea.mult(a))
End Sub
Sub arearect()
Dim x, y As Integer
Dim objRectangleArea As New RectangleArea()
Console.WriteLine("What is the length of the rectangle?")
x = Int32.Parse(Console.ReadLine)
Console.WriteLine("What is the width of the rectangle?")
y = Int32.Parse(Console.ReadLine)
Console.WriteLine("The area of the rectangle is " & objRectangleArea.mult(x, y))
End Sub
Public Sub addition()
Console.WriteLine("the combined areas are equal to " & x * y + a * a)
End Sub
End Module
I am multiplying two sets of numbers using variables from the user.
Then I want to take the products of both operations and add them together.
So I want to use the variables from other subs. How would I do that?

Take out
Dim a As Integer
in Public Sub areaSquare()
AND
Dim x, y As Integer
in Sub areaRect().
Module Module1
Dim a, x, y As Integer
Public Delegate Sub Areas()
Sub Main()
Dim Square As Areas
Dim Rectangle As Areas
Dim Add As Areas
Dim Area As [Delegate]
Square = New Areas(AddressOf areasquare)
Rectangle = New Areas(AddressOf arearect)
Add = New Areas(AddressOf addition)
Area = [Delegate].Combine(Square, Rectangle, Add)
Area.DynamicInvoke()
Console.WriteLine("the combined areas are equal to " & x * y + a * a)
Console.ReadKey()
End Sub
Class SqareArea
Public Function mult(ByVal a As Integer)
Return a * a
End Function
End Class
Class RectangleArea
Public Function mult(ByVal x As Integer, ByVal y As Integer)
Return x * y
End Function
End Class
Public Sub areasquare()
Dim objSqareArea As New SqareArea()
Console.WriteLine("What is the measurement of one side of the square?")
a = Int32.Parse(Console.ReadLine)
Console.WriteLine("The area of the square is " & objSqareArea.mult(a))
End Sub
Sub arearect()
Dim objRectangleArea As New RectangleArea()
Console.WriteLine("What is the length of the rectangle?")
x = Int32.Parse(Console.ReadLine)
Console.WriteLine("What is the width of the rectangle?")
y = Int32.Parse(Console.ReadLine)
Console.WriteLine("The area of the rectangle is " & objRectangleArea.mult(x, y))
End Sub
Public Sub addition()
Console.WriteLine("the combined areas are equal to " & x * y + a * a)
End Sub
End Module
You're declaring class level variables, but then diminishing their value during run-time.
My inputs:
Side of square: 5
Length of rectangle: 10
Width of rectangle: 4
Results:
Area of the square is 25
Area of the rectangle is 40
The combined areas are equal to 65
The combined areas are equal to 65

Related

Attach VBA Class Object to Visio shape

Question
I have a shape in visio 2021 , which is the "GRID" found in "Charting Shapes"
I would like to scale the smaller shapes in the master according to the ratios. Therefore I would like to bind a new instance of the class I created below to my master, and then be able to resize the master, which intern would scale the components relative to the ratios.
Code
Class name = LWR_Calc
Private Widths() As Double
Private Heights() As Double
Private W, H As Double
Private TotalWidthRatio, TotalHeightRatio
Private WidthRatioSubDivision, HeightRatioSubDivision
Private Sub Class_Initialize()
W = 1
H = 1
End Sub
Public Sub SetWidths(Lst As String, Optional delimiter As String = ",")
Dim WidthsRatioStrArr() As String
Dim Current As Double
WidthsRatioStrArr = Split(Lst, delimiter)
TotalWidthRatio = 0
ReDim Widths(0 To UBound(WidthsRatioStrArr))
For i = 0 To UBound(WidthsRatioStrArr)
Current = CDbl(WidthsRatioStrArr(i))
Widths(i) = Current
TotalWidthRatio = TotalWidthRatio + Current
Next
WidthRatioSubDivision = W / TotalWidthRatio
End Sub
Public Sub SetHeights(Lst As String, Optional delimiter As String = ",")
Dim HeightsRatioStrArr() As String
Dim Current As Double
HeightsRatioStrArr = Split(Lst, delimiter)
TotalHeightRatio = 0
ReDim Heights(0 To UBound(HeightsRatioStrArr))
For i = 0 To UBound(HeightsRatioStrArr)
Current = CDbl(HeightsRatioStrArr(i))
Heights(i) = Current
TotalHeightRatio = TotalHeightRatio + Current
Next
HeightRatioSubDivision = H / TotalHeightRatio
End Sub
Public Function GetHeight(ByVal index As Integer) As Double
On Error GoTo endr:
GetHeight = Heights(index - 1) * HeightRatioSubDivision
Exit Function
endr:
GetHeight = 0
End Function
Public Function GetWidth(ByVal index As Integer) As Double
On Error GoTo endr:
GetWidth = Widths(index - 1) * WidthRatioSubDivision
Exit Function
endr:
GetWidth = 0
End Function
Public Property Let Width(ByVal vNewValue As Double)
W = vNewValue
End Property
Public Property Let Height(ByVal vNewValue As Double)
H = vNewValue
End Property
my sub which tests the code is as follows
Private Sub Test__LWR_Calc()
Dim LWRC As LWR_Calc
Set LWRC = New LWR_Calc
LWRC.Height = 2
LWRC.Width = 10
LWRC.SetWidths ("1.75,1,1,1,1,1,1,1,1,1")
LWRC.SetHeights ("1.75,1,1,1.75,1,1,1,1,1,1")
For i = 1 To 10
For j = 1 To 10
Debug.Print i & "-" & j & " "; LWRC.GetWidth(j) & " , " & LWRC.GetHeight(i)
Next
Next
Set LWRC = Nothing
End Sub
This code works to get the values below
Data
Output
The Outputs I Get vs the Output I Want.

vb.net calculation doesn't give decimals

hello im trying to do this calculation : [365!/((365^x)((365-x)!))]
the problem is when i do it it doesn't give me the decimals just the integer it give me 0 or 1 because the answer is 0
Public Class Form1
Private Function fact(ByVal n As Integer) As Numerics.BigInteger
Dim Z As New Numerics.BigInteger(1)
For i As Integer = 1 To n
Z = Z * i
Next
Return Z
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim min As Integer
Dim max As Integer
Dim ranum As Integer
Dim ind() As Integer
Dim ran As New Random
Dim F365 As New Numerics.BigInteger(0)
F365 = Numerics.BigInteger.Parse("25104128675558732292929443748812027705165520269876079766872595193901106138220937419666018009000254169376172314360982328660708071123369979853445367910653872383599704355532740937678091491429440864316046925074510134847025546014098005907965541041195496105311886173373435145517193282760847755882291690213539123479186274701519396808504940722607033001246328398800550487427999876690416973437861078185344667966871511049653888130136836199010529180056125844549488648617682915826347564148990984138067809999604687488146734837340699359838791124995957584538873616661533093253551256845056046388738129702951381151861413688922986510005440943943014699244112555755279140760492764253740250410391056421979003289600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
min = Integer.Parse(Tmin.Text)
max = Integer.Parse(Tmax.Text)
ranum = Integer.Parse(TRan.Text)
ReDim ind(ranum)
For x As Integer = 1 To ranum
ind(x) = ran.Next(min, max + 1)
Answer.Items.Add(ind(x))
Next
Dim P(ranum) As Numerics.BigInteger
Dim facts(ranum) As Numerics.BigInteger
For x = 1 To ranum
P(x) = 365 ^ (ind(x))
facts(x) = fact(365 - ind(x))
Next
Dim phenB(ranum) As Numerics.BigInteger
Dim phen(ranum) As Double
For x = 1 To ranum
phenB(x) = (P(x) * facts(x))
phen(x) = F365 / phenB(x)
tx.Text = phen(x) (here is the aswer)
Next
End Sub
End Class
The BigInteger class does not have a function to give a non-integer result for division. However, it does have BigInteger.Log, so, using these logarithmic identities:
ln(a⋅b) = ln(a) + ln(b)
ln(a/b) = ln(a) - ln(b)
ln(a^b) = b⋅ln(a)
we can perform the calculation like this:
Function SomeCalc(n As Integer) As Double
Dim lnF365 = BigInteger.Log(fact(365))
Dim lnPower = n * Math.Log(365)
Dim lnOtherFact = BigInteger.Log(fact(365 - n))
Return Math.Exp(lnF365 - lnPower - lnOtherFact)
End Function
where fact() is a pre-calculated array:
Option Strict On
Option Infer On
' ... other code ...
Dim fact(365) As BigInteger
' ... other code ...
Private Sub CalcFacts()
Dim z = BigInteger.One
For i = 1 To 365
z *= i
fact(i) = z
Next
End Sub
You could even have an array of pre-calculated logs of the factorials, instead of an array of the factorials. It depends on if you're using them elsewhere, and if there is any need for it to go a tiny tiny bit faster:
Function SomeCalc(n As Integer) As Double
Dim lnF365 = lnFact(365)
Dim lnPower = n * Math.Log(365)
Dim lnOtherFact = lnFact(365 - n)
Return Math.Exp(lnF365 - lnPower - lnOtherFact)
End Function
and
Dim lnFact(365) As Double
' ...
Private Sub CalcLnFacts()
Dim z = BigInteger.One
For i As Integer = 1 To largestNum
z *= i
lnFact(i) = BigInteger.Log(z)
Next
End Sub
That number 365 should be a named variable - I had no idea what a sensible name for it would be.

How to create circe whit GMap in VB.Net

I'm looking for some function which can create a circle on map. I'm using Gmap library in VB.Net. Exist some function which can create a circle around of point on map, for examle with 500 meters radius ?
I found a code, but it isn't what I'm exactly looking for :
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports GMap.NET
Imports GMap.NET.WindowsForms
Namespace Map
Public Class GMapMarkerCircle
Inherits GMapMarker
Private m_Radius As Integer
'In Meters
Public m_OutlinePen As Pen
Public m_FillBrush As Brush
Public m_Fill As Boolean
Public Sub New(p As PointLatLng, Radius As Integer, OutlinePen As Pen, FillBrush As Brush, Fill As Boolean)
MyBase.New(p)
m_OutlinePen = OutlinePen
m_FillBrush = FillBrush
m_Radius = Radius
m_Fill = Fill
End Sub
Public Overrides Sub OnRender(g As Graphics)
g.SmoothingMode = SmoothingMode.AntiAlias
Dim R As Integer = CInt((m_Radius) / Overlay.Control.MapProvider.Projection.GetGroundResolution(Overlay.Control.Zoom, Position.Lat)) * 2
If m_Fill = True Then
g.FillEllipse(m_FillBrush, New System.Drawing.Rectangle(LocalPosition.X - R \ 2, LocalPosition.Y - R \ 2, R, R))
End If
g.DrawEllipse(m_OutlinePen, New System.Drawing.Rectangle(LocalPosition.X - R \ 2, LocalPosition.Y - R \ 2, R, R))
End Sub
End Class
End Namespace
And in app:
Dim CircleMarker As New GMapMarkerCircle(New GMap.NET.PointLatLng(ZemSirka, ZemDlzka), 660, New Pen(Color.Azure, 1), Brushes.LightSeaGreen, True)
Dim Overlay As New GMapOverlay("Circle")
Overlay.Markers.Add(CircleMarker)
GMapControl1.Overlays.Add(Overlay)
But when I zoom in/out the map circle disappears.
I have one beginners question: is any possibility to do Brushes semitransparent ?
Finally I create code which works fine for short distances for examle: 200,300,500 meters.
Public Function toRad(ByVal deegres As Double)
Return deegres * (Math.PI / 180)
End Function
Public Function toDeg(ByVal radians As Double)
Return radians * (180 / Math.PI)
End Function
Private Function pivotRadius(ByVal Dlzka As Double, ByVal Sirka As Double, ByVal dist As Double)
Dim distance = (dist / earthRadius) / 1000
Dim lat As Double = toRad(Sirka)
Dim lng As Double = toRad(Dlzka)
Dim points As IList(Of PointLatLng) = New List(Of PointLatLng)()
Dim x As Integer = 0
While x < 360
Dim brng As Double = toRad(x)
Dim latRadians As Double = Math.Asin(Math.Sin(lat) * Math.Cos(distance) + Math.Cos(lat) * Math.Sin(distance) * Math.Cos(brng))
Dim lngRadians As Double = lng + (Math.Atan2(Math.Sin(brng) * Math.Sin(distance) * Math.Cos(lat), Math.Cos(distance) - Math.Sin(lat) * Math.Sin(latRadians)) / 1.6)
points.Add(New PointLatLng(toDeg(lngRadians), toDeg(latRadians)))
latRadians = 0
lngRadians = 0
x += 10
End While
Dim polyOverlay As New GMapOverlay("polygons")
Dim polygon As New GMapPolygon(points, "mypolygon")
polygon.Fill = New SolidBrush(Color.FromArgb(30, Color.Aqua))
polygon.Stroke = New Pen(Color.Blue, 1)
GMapControl1.Overlays.Clear()
GMapControl1.Overlays.Add(polyOverlay)
polyOverlay.Polygons.Add(polygon)
Return 0
End Function

VB.NET Compare each item in collection to every other item in collection - Threading

this is my first time posting so please accept my apologies if im not doing this right and please feel free to correct me for any formatting or posting guidelines. I am doing this in VB.Net with .NET Framework 4.5.2.
I have a large collection called gBoard in a class.
Private gBoard As Collection
It contains roughly 2000 instances of a class.
What i am trying to achieve is for each item in the class, i want to look at each other item in the class and then update the first item based on variables in the second.
Currently i have the following code:
In the main class:
Private gBoard As New Collection ' This is populated elsewhere in the code
Private Sub CheckSurroundings()
For i As Integer = 1 To (xBoxes)
For j As Integer = 1 To (yBoxes)
For x = 1 As Integer To (xBoxes)
For y = 1 As Integer To (yBoxes)
Tile(New Point(i, j)).CheckDistance(Tile(New Point(x, y)))
Next y
Next x
Next j
Next i
End Sub
Private Function Tile(ByVal aPoint As Point) As clsTile
Return gBoard.Item("r" & aPoint.Y & "c" & aPoint.X)
End Function
In clsTile i have the following (as well as other items):
Private Function SurroundingTerrain(ByVal aTer As String) As clsTerrain
Return mySurroundings.Item(aTer) ' a small collection (6 items of clsTerrain type)
End Function
Public Sub CheckDistance(ByRef aTile As clsTile)
SurroundingTerrain(aTile.Terrain).CheckDistance(CalcDistance(Location, aTile.Location))
End Sub
Private Function CalcDistance(ByVal myPoint As Point, ByVal aPoint As Point) As Double
Dim myReturn As Double = 0
Dim xDiff As Integer = 0
Dim yDiff As Integer = 0
Dim tDiff As Integer = 0
xDiff = Math.Abs(myPoint.X - aPoint.X)
yDiff = Math.Abs(myPoint.Y - aPoint.Y)
tDiff = xDiff + yDiff
myReturn = (MinInt(xDiff, yDiff) * 1.4) + (tDiff - MinInt(xDiff, yDiff))
Return myReturn
End Function
Private Function MinInt(ByVal a As Integer, ByVal b As Integer) As Integer
Dim myReturn As Integer = a
If b < myReturn Then
myReturn = b
End If
Return myReturn
End Function
in clsTerrain i have the following sub that is called:
Public Sub CheckDistance(ByVal aDist As Double)
If aDist < Distance Then
Distance = aDist
End If
End Sub
This runs and works file but as you can guess it runs so slow... I have been trying to work out how to make this run faster and i looked into threading/tasks but it doesnt seem to work. There are no errors but the objects don't appear to update correctly (or at all). The code i tried was:
In the main class:
Private Sub CheckSurroundings()
Dim tasks As New List(Of Task)
Dim pTile As clsTile
For Each pTile In gBoard
tasks.Add(Task.Run(Sub() TileToCheck(pTile)))
Next
Task.WaitAll(tasks.ToArray())
End Sub
Private Sub TileToCheck(ByRef aTile As clsTile)
For x As Integer = 1 To (xBoxes)
For y As Integer = 1 To (yBoxes)
aTile.CheckDistance(Tile(New Point(x, y)))
Next y
Next x
End Sub
Does anyone have any suggestions or ideas for how to get this to work?
Sorry for any headaches or facepalms caused...

Visual basic : Calculate Average function returning infinity

I am supposed to calculate the average number of words and then convert them into a percentage, however they are displaying infinity. The code I am using is below .
Public Class Form1
Private Structure advertisements
Public name As String
Public words As Integer
Public font() As fonts
Public mostLegible As String
End Structure
Private Structure fonts
Public name As String
Public NoWords() As Integer
Public aveWords As Double
Public percent As Double
End Structure
Private noAdverts As Integer
Private noFonts As Integer
Private noReaders As Integer
Private advert() As advertisements
Private Sub GridPlacement(ByVal r As Integer, ByVal c As Integer, ByVal t As String)
grdDisplay.Row = r
grdDisplay.Col = c
grdDisplay.Text = t
End Sub
Private Sub gridAndArraySettings()
Dim x, y As Integer
ReDim advert(noAdverts)
For x = 1 To noAdverts
ReDim advert(x).font(noFonts)
For y = 1 To noFonts
ReDim advert(x).font(y).NoWords(noReaders)
'GridPlacement(0, y, "font " & CStr(y))
Next
Next
grdDisplay.Cols = noFonts + 2
grdDisplay.Rows = noAdverts + 1
GridPlacement(0, 0, "Name of advert")
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
noAdverts = CInt(txtadverts.Text)
noReaders = CInt(txtReaders.Text)
noFonts = CInt(txtFonts.Text)
gridAndArraySettings()
End Sub
Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
Dim x, y, z As Integer
Dim total(,) As Integer
ReDim total(noAdverts, noFonts)
For x = 1 To noAdverts
advert(x).name = InputBox("please enter the name of advert " & CStr(x))
advert(x).words = CInt(InputBox("please enter the number of words for " & advert(x).name))
GridPlacement(x, 0, advert(x).name)
For y = 1 To noFonts
advert(x).font(y).name = InputBox("please enter the name of font " & CStr(y) & " Used in advert " & CStr(x))
For z = 1 To noReaders
advert(x).font(y).NoWords(z) = CInt(InputBox("please enter the number of words understood by reader " & CStr(z) & " for font " & advert(x).font(y).name & " in advert " & advert(x).name))
total(x, y) += advert(x).font(y).NoWords(z)
Next
advert(x).font(y).aveWords = calcAve(total(x, y))
advert(x).font(y).percent = advert(x).font(y).aveWords * 100
GridPlacement(x, y, CStr(advert(x).font(y).aveWords)) 'Format(advert(x).font(y).percent, "###%")
Next
Next
End Sub
Private Function calcAve(ByVal elements As Integer) As Double
Dim x, y As Integer
Dim placeholder As Double = 1
Select Case elements
Case Is > 0
For x = 1 To noAdverts
For y = 1 To noFonts
advert(x).font(y).aveWords = (elements / noReaders) / advert(x).words
placeholder = advert(x).font(y).aveWords
MsgBox(CStr(placeholder))
Next y
Next x
Case Is = 0
MsgBox("There is incorrect values ")
End Select
Return placeholder
End Function
End Class
The textboxes are called txtadverts, txtfonts and txtreaders
The button btnsubmit gets the input from the textboxes and stores them in variables. btninput allows input from user.
GrdDisplay is a custom extension used by my university which is a 2D grid used to diplay values.