Working out a basic score from integer values - vb.net

I'm trying to work out a basic % value by gathering values together:
For Each M As Match In matchFound
lamdaFix2 = M.Groups(1).Value
lamdaFix3 = M.Groups(2).Value
Application.DoEvents()
Dim lv As ListViewItem = formMozCheck.listViewMoz.Items.Add(lamdaFix1)
lv.UseItemStyleForSubItems = False
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix2)).ToString())
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix3)).ToString())
lv.SubItems.Add("-")
lv.SubItems.Add("-")
lv.SubItems.Add(itm.SubItems(8).Text)
Dim srVal As Integer
If (itm.SubItems(9).Text = "") Then
srVal = 0
Else
srVal = CInt(itm.SubItems(9).Text)
End If
lv.SubItems.Add(srVal.ToString())
' work out a score
Dim overAllScore As Integer
' TODO: basic score
overAllScore = CInt(CInt(CDbl(Math.Round(Double.Parse(lamdaFix2)).ToString()) & CDbl(Math.Round(Double.Parse(lamdaFix3)).ToString()) & CDbl(itm.SubItems(8).Text) & Val(srVal)) * 100)
lv.SubItems.Add(CLng(overAllScore) & "%").ForeColor = Color.DarkGreen
itm.Checked = False
Next
These values:
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix2)).ToString())
lv.SubItems.Add(Math.Round(Double.Parse(lamdaFix3)).ToString())
Are values between 0 - 100
And these ones:
itm.SubItems(8).Text
itm.SubItems(9).Text
Are values between 1 - 10
I have been racking the brains to see what the best way to add these values up and give a basic percentage, my attempt:
overAllScore = CInt(CInt(CDbl(Math.Round(Double.Parse(lamdaFix2)).ToString()) & CDbl(Math.Round(Double.Parse(lamdaFix3)).ToString()) & CDbl(itm.SubItems(8).Text) & Val(srVal)) * 100)
Just adds all values like 234524 rather than the total value of 23+45+2+4
i'm probably over complicating this lol
i tagged it as both vb and c# as i though the logic would be the same ;)
thanks for any help guys!
Graham

You should use "+" instead of "&":
overAllScore = CInt(CInt(CDbl(Math.Round(Double.Parse(lamdaFix2)).ToString()) + CDbl(Math.Round(Double.Parse(lamdaFix3)).ToString()) + CDbl(itm.SubItems(8).Text) + Val(srVal)) * 100)

Related

Ascending Order Textbox Separated with Comma

I'm new here. I have a textbox,
Textbox1.text = 1,2,7,4,11.
I want to be Output:
1,2,4,7,11.
Textbox1.text = 1,2,7,4,11.
I want to be Output:
1,2,4,7,11.
VB.Net
I found this code and it works for who wants it.
Code:
Private Sub Array()
Dim InputNumbers, SplitInputNumbers, ArrayCount, ReSort, iterInputNum, Num1, Num2
InputNumbers = OutputText1.Text
SplitInputNumbers = Split(InputNumbers, ",")
ArrayCount = UBound(SplitInputNumbers)
ReSort = "YES"
While ReSort = "YES"
ReSort = "NO"
For iterInputNum = 0 To ArrayCount
If iterInputNum < ArrayCount Then
If CInt(SplitInputNumbers(iterInputNum)) > CInt(SplitInputNumbers(iterInputNum + 1)) Then
Num1 = SplitInputNumbers(iterInputNum)
Num2 = SplitInputNumbers(iterInputNum + 1)
SplitInputNumbers(iterInputNum + 1) = Num1
SplitInputNumbers(iterInputNum) = Num2
ReSort = "YES"
End If
End If
Next
End While
Dim iterSortedNum, SortedNumericArray
For iterSortedNum = 0 To ArrayCount
If iterSortedNum = 0 Then
SortedNumericArray = SplitInputNumbers(iterSortedNum)
Else
SortedNumericArray = SortedNumericArray & "," & SplitInputNumbers(iterSortedNum)
End If
Next
OutputText1.Text = (SortedNumericArray)
You could do something like this. It takes your string splits it into an array. Converts each substring into a number, Making a new Integer array. Sorts that new array. and then using join converts it back into a comma separated string
Dim str = "1,2,7,4,11"
Dim b = String.Join(",", str.Split(",").Select(Function(x) Integer.Parse(x.Trim())).OrderBy(Function(x) x))

Update previous row

I have an Excel file that contains some datas that I want to export into an Access db. In the C column I've got a field called 'Description'. Usually this field occupy just one cell, but it can happens that is more long.
In this case, for example, AP.01 has got 5 rows of description. How can update the first row with the next rows?
Public Sub updateDB(ByVal PathDB As String, str As String, id As Integer)
Dim db As New cDB
Dim v As New cVoce
Dim rs As ADODB.Recordset = db.RecordSet
v.Description = str
db.connetti_DB(PathDB)
db.get_rs("UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'")
End Sub
Public Function get_rs(ByVal query As String) As ADODB.Recordset
If db Is Nothing Then rs = Nothing : Return rs
rs = New ADODB.Recordset
rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
rs.LockType = ADODB.LockTypeEnum.adLockOptimistic
rs.Open(query, db)
Return rs
End Function
This code doesn't work because I update my current row, for this reason is useless the UPDATE instruction. How can I fix my code?
EDIT I post here the For loop
For r = 2 To grid.RowCount - 1
vett = Split(grid(r, 1).Text)
total = UBound(Split(grid(r, 1).Text, "."))
If grid(r, 1).Text <> "" Then
Select Case total
Case 0
Dim chapter As New cChapter
flag = 1
id = id + 1
chapter.Cod = grid(r, 1).Text.Substring(0, 1)
chapter.Description = grid(r, 3).Text
If Left(vett(0), 1) >= Chr(65) And Left(vett(0), 1) <= Chr(90) Then
chapter.Cod = Left(vett(0), 1)
oldChap = chap.Cod
If chapter.Cod <> oldCap Then
chapters.Add(chapter)
End If
End If
chapters.Add(chapter)
stringChap = chap.Description
Dim par As New cParagraph
If Left(vett(0), 2) >= Chr(65) And Left(vett(0), 2) <= Chr(90) Then
par.Cod = Left(vett(0), 2)
par.Cod_Chapter = Left(vett(0), 1)
oldPar = par.Cod
If par.Cod <> oldPar Then
paragraphs.Add(par)
End If
End If
If grid(r, 3).Text.Length > 255 Then
par.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
par.Description = grid(r, 3).Text.ToString
End If
paragraphs.Add(par)
stringPar = par.Description
Case 1
flag = 2
id = id + 1
c_Voc = voc.Cod_Chapter
p_Voc = voc.Cod_Paragraph
voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1)
voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0)
voc.Cod_Voice = Right(vett(0), 2)
If grid(r, 3).Text.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
voc.Description = grid(r, 3).Text.ToString
If voc.Description.EndsWith("-") Then
a = Replace(voc.Description, "-", "")
voc.Description = a
End If
End If
stringVoice = voc.Description
voices.Add(voc)
voices.Save_DB(dbDest)
Case 2
flag = 3
id = id + 1
sVoice = New cVoice
oldSvoice = voice.Cod_SVoice
sVoice.Cod_SVoice = Left(vett(0), 2)
If sVoice.Cod_SVoce <> oldSvoice Then
voices.Add(sVoice)
voices.Save_DB(dbDest)
End If
If grid(r, 3).Text.Length > 255 Then
sVoice.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
sVoice.Description = grid(r, 3).Text
End If
stringSvoice = sVoice.Description
sVoice.Cod_Voce = Left(vett(0), 5)
sVoice.Price1 = grid(r, 12).Text
sVoice.Price2 = sVoice.Price1
sVoice.UniMi = grid(r, 11).Text
sVoce.Sep = "."
voices.Add(sVoce)
voices.Save_DB(dbDest)
End Select
Else
If flag = 1 Then
stringChap = grid(r, 3).Text
chap.Description = stringChap & grid(r, 3).Text
stringPar = grid(r, 3).Text
paragraph.Description = stringPar & grid(r, 3).Text
End If
If flag = 2 Then
stringVoice = grid(r, 3).Text
voc.Description = voc.Description & stringVoice
voices.updateDB(dbDest, stringVoice, id)
voices.Add(voc)
End If
If flag = 3 Then
stringSvoice = grid(r, 3).Text
sVoice.Description = stringSvoice & grid(r, 3).Text
voices.Add(sVoice)
End If
chapter.Save_DB(dbDest)
paragraph.Save_DB(dbDest)
voice.Save_DB(dbDest)
End If
Next
EDIT2 I declared id As Integer and when Code column has a value then id=id+1. In this way I always know which row I have to modify. I modified also updateDB (now I'm using 3 parameters) and I added a WHERE condition into my query. Despite the update, nothing has changed
In database you cannot store records without PrimaryKey (actually you can, but it is bad idea). Since in your solution id is in fact Excel row number (sorry if I'm not correct but it looks like from code) it could be very hard to maintain it in future (in case someone add or remove description row). It would be better to change id column to text and use code as PK.
Storing description then could be solved in 2 ways:
1) Concatenate all rows containing description into 1 variable adding vbNewLine in between and store it in description field.
2) More relational but also more complex - create 2nd table for description with PK as i.e. autonumber, ForeignKey Code referring to main table. Maintenance will be here very complex. Not really worth effort.
Amount of changes in code is quite big, so sorry I'll not provide fixed code but I hope idea is clear.
BTW: The reason why description is not updated is described in your post. You are increasing id only when code is present, so every description field from first group have id = 1. The most simple fix in your code would be to create 2 update statements - One for rows with code
UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'
Second one for rows without code:
UPDATE Voice SET Description = Description + CHAR(13) + CHAR(10) + '" + v.Description + "' WHERE id= '"+id+"'

VB - comparing numbers in two labels

I'm doing a school project in Visual Basic (using visual studio 2015) and i'm kinda stuck.
My goal is to create a lottery, where player chooses 6 numbers from checkboxes, then he generates six random numbers (1 - 49) and finally, those two sets should be compared and needed result is the number of correctly guessed numbers.
I have both results (guessed numbers, generated numbers) saved in two different labels.
The checkboxes itself are genereted like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lev = 20
tt = 0
For j = 1 To 50
tt = tt + 1
n = n + 1
box(j) = New CheckBox
box(j).Name = "box(" & Str(j) & ")"
If n = 11 Then lev = lev + 110 : n = 1 : tt = 1
box(j).Left = lev
box(j).Parent = Me
box(j).Top = tt * 20
box(j).Tag = j
box(j).Text = j
box(j).Visible = True
Next
box(50).Enabled = False
End Sub
First label (guessed numbers) is filled this way (i'm not posting whole code)
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
and the second one (generated numbers) like this:
Do
rn = rg.Next(1, 50)
If Not r.Contains(rn) Then
r.Add(rn)
End If
Loop Until r.Count = 6
Label1.Text = r(0).ToString + " " + r(1).ToString + " " + r(2).ToString + " " + r(3).ToString + " " + r(4).ToString + " " + r(5).ToString
any idea how to compare numbers stored in those labels and get the result (number of correctly guessed numbers).
thanks in advance
You can compare numbers in the labels by splitting the Text properties of the labels into arrays of strings and converting them to integer arrays. First though there is a tiny problem with your code that adds the guessed numbers to the label.
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
The " " should be moved to the end of the line because at the moment, the label will always start with a space and that messes with the function below. So you should have -
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + box(j).Text + " "
Next
Ok. The function below splits the two text labels into their own array and loops through the guesses and checks if any number is contained in the generated numbers. It then returns the number of matches.
Private Function ComparePicks() As Integer
Dim numbersMatched As Integer
Dim picks(5) As Integer
Dim generatedNumbers(5) As Integer
For i As Integer = 0 To 5
picks(i) = CInt(Split(Label9.Text, " "c)(i))
Next
For i As Integer = 0 To 5
generatedNumbers(i) = CInt(Split(Label1.Text, " "c)(i))
Next
For i As Integer = 0 To 5
If generatedNumbers.Contains(picks(i)) Then
numbersMatched += 1
End If
Next
Return numbersMatched
End Function

Visual Basic - How to concatenate a string in a while loop

I've been working on a homework problem for a few hours now and cannot for the life of me figure out how to get it to work.
The premise of the program is:
-User inputs number
-Display a multiplication table from 1-9 of that users number
if they entered 4 I need to show
1 * 4 = 4
2 * 4 = 8
.
.
.
9 * 4 = 36
Here is what I (unsuccessfully) have for my loop so far:
Dim userNum As Integer = numInputTextBox.Text
Dim tableDisplay As String
Dim i As Integer = 1
Do While i <= 9
tableDisplay = userNum * i
tableTextBox.Text = tableDisplay
i += 1
Loop
(tableTextBox is where the output will be stored)
The only output I get is the result of the last iteration of the loop so if I entered 7, the output I get is 63 (7 * 9)
I'm unsure how to continually add to a string and output it to the textbox while in a loop.
Any and all help is greatly appreciated. My apologies for the sloppy formatting.
Here is an idea of what I am talking about in my comment:
Dim userNum As Integer = numInputTextBox.Text
Dim tableDisplay As String
Dim i As Integer = 1
Do While i <= 9
tableDisplay = userNum & " * " & i & " = " & userNum * i & vbLf
i += 1
Loop
tableTextBox.Text = tableDisplay
Although I wouldn't use a while loop in this instance, I would use a for next like so:
Dim userNum As Integer = numInputTextBox.Text
Dim tableDisplay As String
Dim i As Integer
For i = 1 To 9
tableDisplay = userNum & " * " & i & " = " & userNum * i & vbLf
Next
tableTextBox.Text = tableDisplay

My random quiz generator is returning two questions

my objective is to generate a random question ( +, * or -) and an answer when I click a button which I have been able to do. The problem is when I generate it, it returns a question(label) and an answer(MessageBox) but when I click ok on the messageBox it asks another question straight after and once I dismiss that messageBox it stops asking.
Private Sub Generate()
'This is my first random value'
Dim rnd1 As New Random
'This is my Second random Value'
Dim rnd2 As New Random
'This value will decide weather it is a +, * or -'
Dim rnd3 As New Random
'Declaring first value as an integer'
Dim Val1 As Integer
'Declaring second value as an integer'
Dim Val2 As Integer
'This will calculate the answer'
Dim Ans As Double
'This is what I will reference to to display the question'
Dim question As String
'If the random value is equal to 1, the question is an addition'
If (rnd3.Next(1, 3) = 1) Then
Val1 = rnd1.Next(1, 20)
Val2 = rnd2.Next(1, 25)
Ans = Val1 + Val2
question = Val1.ToString() + "+ " + Val2.ToString()
lbl_ques.Text = question
MessageBox.Show("Answer = " + Ans.ToString())
Val1 = 0
Val2 = 0
Ans = 0
question = ""
End If
'If the random value is equal to 2, the question is a multiplication'
If (rnd3.Next(1, 3) = 2) Then
Val1 = rnd1.Next(1, 10)
Val2 = rnd2.Next(1, 17)
Ans = Val1 * Val2
question = Val1.ToString() + "* " + Val2.ToString()
lbl_ques.Text = question
MessageBox.Show("Answer = " + Ans.ToString())
Val1 = 0
Val2 = 0
Ans = 0
question = ""
End If
'If the random value is equal to 3, the question is a subtraction'
If (rnd3.Next(1, 3) = 3) Then
Val1 = rnd1.Next(1, 50)
Val2 = rnd2.Next(1, 43)
Ans = Val1 - Val2
question = Val1.ToString() + "- " + Val2.ToString()
lbl_ques.Text = question
MessageBox.Show("Answer = " + Ans.ToString())
Val1 = 0
Val2 = 0
Ans = 0
question = ""
End If
End Sub
How can I stop it from generating another question and answer straight away? Thanks
P.S If you are wondering why I am using VB it is because its for a school project and has to be in VB :/ I usually use c#
Either use
An exit sub to break out of the current subroutine
If ... Then
Exit Sub
End If
or and ElseIf to only execute one of the If conditions
If ... Then
ElseIf ... Then
End If
or (preferably) a Select case statement
Select Case rnd.Next(1, 3)
Case 1
Case 2
Case 3
End Select
Side note you only need to create one Random object and just call .Next with the appropriate values each time