Listbox Values calculate - vb.net

This resets the text box for quantity and amount to zero when i test, how do i make this calculate?
Dim Number As Integer
txtNumber.Text = Number
Dim orchestra As Integer = 40
Dim mezz As Integer = 27.5
Dim general As Integer = 15
Dim balcony As Integer = 10
If lstSection.SelectedItem = "Orchestra" Then
txtAmount.Text = (Number * 40)
ElseIf lstSection.SelectedItem = "Mezzanine" Then
txtAmount.Text = (Number * 27.5)
ElseIf lstSection.SelectedItem = "General" Then
txtAmount.Text = (Number * 15)
ElseIf lstSection.SelectedItem = "Balcony" Then
txtAmount.Text = (Number * 10)
End If

Assuming my edit is correct, and you intend to declare your Number variable within the same method, the problem you're seeing is occurring because you're not assigning a value to Number.
Integer value types always initialize with the default value of 0.
Thus: txtAmount.Text = (0 * 40), which of course will always be 0.

Related

Find First and Last Number in a Range of Numbers

Imagine a range of numbers from -133 to +71.
I want to find the first and last numbers in the range that divide by 20: in this case it would be -120 and +60.
I can write a For loop that tests each value and stores the required values:
Dim resultFirst, resultLast As Integer
Dim FirstFound As Boolean = False
For a As Integer = -133 To 71
If a Mod 20 = 0 Then
If FirstFound = False Then
resultFirst = a
FirstFound = True
End If
resultLast = a
End If
Next
but I suspect there is a simpler formula.
You can use Enumerable.Range() and the LINQ-methods Where, Min and Max
Dim resultFirst As Integer
Dim resultLast As Integer
Dim min As Integer = -133
Dim max As Integer = 71
Dim div As Integer = 20
resultFirst = Enumerable.Range(min, max - min + 1).Where(Function(x) x Mod div = 0).Min()
resultLast = Enumerable.Range(min, max - min + 1).Where(Function(x) x Mod div = 0).Max()
Try this one
Dim s As IEnumerable(Of Integer) =
Enumerable.Range(-133, 133 + 72)
Dim minV As Integer = s.AsEnumerable().Where(Function(n) n Mod 20 = 0).Min(Function(n) n)
Dim maxV As Integer = s.AsEnumerable().Where(Function(n) n Mod 20 = 0).Max(Function(n) n)
Console.WriteLine(minV.ToString() & " " & maxV.ToString())
Console.ReadLine()
You can use the following to get the first and last value which is dividable by 20:
Dim fromValue As Integer = -133
Dim first As Integer = (fromValue - (fromValue Mod 20)) + IIf(fromValue > 0 And fromValue Mod 20 <> 0, 20, 0)
Dim toValue As Integer = 71
Dim last As Integer = (toValue - (toValue Mod 20)) - IIf(toValue < 0 And toValue Mod 20 <> 0, 20, 0)
You can also create a function using the above formula:
Private Function GetResult(ByVal fromInt As Integer, ByVal toInt As Integer, ByVal divider As Integer) As Integer()
'set the real from and to value from parameter.
Dim fromValue As Integer = Math.Min(fromInt, toInt)
Dim toValue As Integer = Math.Max(fromInt, toInt)
'get the first and last number dividable by divider between numbers.
Dim first As Integer = (fromValue - (fromValue Mod divider)) + IIf(fromValue > 0 And fromValue Mod divider <> 0, divider, 0)
Dim last As Integer = (toValue - (toValue Mod divider)) - IIf(toValue < 0 And toValue Mod divider <> 0, divider, 0)
If first > toValue Or last < fromValue Then
Return {}
Else
Return {first, last}
End If
End Function
Some test cases for the above function:
GetResult(-133, 71, 20) '0: -120; 1: 60
GetResult(71, -133, 20) '0: -120; 1: 60
GetResult(100, 119, 20) '0: 100; 1: 100
GetResult(-113, -112, 20) 'empty array
GetResult(120, 140, 20) '0: 120; 1: 140

VBA Split array

I have the following code:
Sub UpdateBlock()
'Define empty variables for each attribute
Dim ent As AcadEntity
Dim oBkRef As AcadBlockReference
Dim Insertpoints As Variant
Dim A As Double
Dim tag As String
Dim material As String
Dim actualLength As String
Dim cutOff As Double
Dim cutLengths As Double
Dim totalLengths As Double
Dim weight As Double
Dim purchaseLength As Double
Dim decimalLength As Double
Dim lengthWeight As Double
Dim totalLengthWeight As Double
Dim cutLengthWeight As Double
Dim cutWeight As Double
Dim order As Double
Dim feet As Double
Dim inches As Double
Dim fraction As Double
Dim fracVal As Variant
'First we go over every object in the modelspace
For Each ent In ThisDrawing.ModelSpace
'Check if the object is a block
If ent.ObjectName = "AcDbBlockReference" Then
Set oBkRef = ent
'If the object is a block then check if its the block we are looking for
If oBkRef.EffectiveName = "AUTOTAG-MATERIAL" Then
A = A + 1
'Get Current Attributes
attlist = oBkRef.GetAttributes
For i = LBound(attlist) To UBound(attlist)
Select Case attlist(i).TagString
Case "ACTUAL-LENGTH"
actualLength = attlist(i).TextString
Case "PURCHASE-LENGTH"
purchaseLength = attlist(i).TextString
Case "CUT-OFF"
cutOff = Frac2Num(attlist(i).TextString)
Case "DECIMAL-LENGTH"
feet = Split(actualLength)(0)
inches = Split(actualLength)(1)
fracVal = Split(actualLength)(2)
If Not IsNull(Split(actualLength)(2)) Then
fraction = Frac2Num(fracVal)
Else
fraction = 0
End If
decimalLength = Round((((feet * 12) + (inches + fraction)) / 12) - cutOff, 2)
attlist(i).TextString = decimalLength
Case "WEIGHT"
weight = attlist(i).TextString
Case "CUT-WEIGHT"
cutWeight = weight * decimalLength
attlist(i).TextString = cutWeight
Case "LENGTH-WEIGHT"
lengthWeight = weight * purchaseLength
attlist(i).TextString = lengthWeight
Case "TOTAL-LENGTHS"
totalLengths = attlist(i).TextString
Case "CUT-LENGTHS"
cutLength = attlist(i).TextString
Case "TOTAL-LENGTH-WEIGHT"
totalLengthWeight = lengthWeight * totalLengths
attlist(i).TextString = totalLengthWeight
Case "CUT-LENGTH-WEIGHT"
totalCutWeight = lengthWeight * cutLength
attlist(i).TextString = totalCutWeight
End Select
Next
End If
End If
Next ent
End Sub
Function Frac2Num(ByVal X As String) As Double
Dim P As Integer, N As Double, Num As Double, Den As Double
X = Trim$(X)
P = InStr(X, "/")
If P = 0 Then
N = Val(X)
Else
Den = Val(Mid$(X, P + 1))
If Den = 0 Then Error 11 ' Divide by zero
X = Trim$(Left$(X, P - 1))
P = InStr(X, " ")
If P = 0 Then
Num = Val(X)
Else
Num = Val(Mid$(X, P + 1))
N = Val(Left$(X, P - 1))
End If
End If
If Den <> 0 Then
N = N + Num / Den
End If
Frac2Num = N
End Function
The variable fraction / fracVal comes from a tag in AutoCAD that is a length, that will always be at least "0 0", but may be "0 0 0" it is a length in feet, inches, and fractional inches. So some possible values could be "8 5", "16 11 11/16", "0 5 3/8" etc.
What I need is a check for when the fraction is not there.
Any suggestions?
I would split the string on the space and see if the ubound of the resulting array is 2. So something like this
If Ubound(Split(thisString, " ")) = 2 then
'fractional part is present
End If
Another option is the Like Operator:
If thisString Like "#* #* #*/#*" Then
# matches any single digit (0–9) and * matches zero or more characters.
but since you split the string anyway, I would store the result of the split in a variable and check the number of items in it with UBound as shown in the other answer.

Automatic Calculation with given numbers

I would like to make CPU to calculate declared result from the given numbers that are also declared.
So far:
Dim ArrayOperators() As String = {"+", "-", "*", "/", "(", ")"}
Dim GlavniBroj As Integer = GBRnb() 'Number between 1 and 999 that CPU needs to get from the numbers given below:
Dim OsnovniBrojevi() As Integer = {OBRnb(), OBRnb(), OBRnb(), OBRnb()} '4 numbers from 1 to 9
Dim SrednjiBroj As Integer = SBRnb() '1 number, 10, 15 or 20 chosen randomly
Dim KrajnjiBroj As Integer = KBRnb() '25, 50, 75 or 100 are chosen randomly
Private Function GBRnb()
Randomize()
Dim value As Integer = CInt(Int((999 * Rnd()) + 1))
Return value
End Function
Private Function OBRnb()
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
Return value
End Function
Private Function SBRnb()
Dim value As Integer = CInt(Int((3 * Rnd()) + 1))
If value = 1 Then
Return 10
ElseIf value = 2 Then
Return 15
ElseIf value = 3 Then
Return 20
End If
Return 0
End Function
Private Function KBRnb()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
Return 25
ElseIf value = 2 Then
Return 50
ElseIf value = 3 Then
Return 75
ElseIf value = 4 Then
Return 100
End If
Return 0
End Function
Is there any way to make a program to calculate GlavniBroj(that is GBRnb declared) with the help of the other numbers (also without repeating), and with help of the given operators? Result should be displayed in the textbox, in a form of the whole procedure of how computer got that calculation with that numbers and operators. I tried to make it work by coding operations one by one, but that's a lot of writing... I'm not looking exactly for the code answer, but mainly for the coding algorithm. Any idea? Thanks! :)

(Visual Basic) Sum of integers through 2 numbers

So im pretty close but I continue to get the wrong values. The user is suppose to enter a positive integer and its suppose to add all the integers in between. So if the user enters 5 it should equal 15, 10 would equal 55, etc. But I get 5 = 25, 10, 100.
Changed to decimal to see if that had anything instead of integer and still did nothing. I saw a few things to set decCount to = 1. Did that and the number was closer but still not there.
Dim decSum As Decimal = 0
Dim decNumber As Decimal = 0
Dim decCount As Decimal = 0
Dim strUserInput As String
strUserInput = InputBox("Enter a positive integer value.", "Input Needed", 0)
If Decimal.TryParse(strUserInput, decNumber) And (decNumber >= 0) Then
Do While decCount < decNumber
decSum = decSum + decNumber
decCount = decCount + 1
Loop
Else
MessageBox.Show("Enter a positive numeric value")
End If
MsgBox("The sum of the numbers 1 through " & decNumber & " is " & decSum)
You are trying to calculate a factorial of a given input, but in your loop you are adding the same number repeatedly (effectively, you are multiplying the number by itself instead of finding the factorial).
Change this line:
decSum = decSum + decNumber
to this:
decSum = decSum + decCount

Do While - Overflow error

This code looks for the column with header "Quantity Dispensed," then convert the strings in the column by treating the right three digits as decimals, e.i. 00009102" = 9.102
Sub ConvertDec()
Dim colNum As Integer
Dim i As Integer
Dim x As Integer
colNum = WorksheetFunction.Match("Quantity Dispensed", ActiveWorkbook.ActiveSheet.Range("1:1"), 0)
i = 2
Do While ActiveWorkbook.ActiveSheet.Cells(i, colNum).Value <> ""
x = Evaluate(Cells(i, colNum).Value)
Cells(i, colNum) = Int(x / 1000) + (x Mod 1000) / 1000
i = i + 1
Loop
End Sub
I'm getting Overflow error on the line "x = Evaluate..." while executing.
The values in the column are in string form. e.g. "0000120000".
120000 is greater than the maximum value of integer 32768. Use the Long type instead.
Simoco