I’ve got a program which starts with welcome screen “Enter the number of the random variables X” and this number refers to the columns. If it is entered 5 => a DataGridView will be created with 5 columns and 2 rows (first one is for random variable entered by the user, and the second is for probability). For example:
X 1 2 3 4 5 6
p 2 2 2 2 2 2
I have to multiply x1 with probability p1, value x2 with probability p2, and so on. So EX = x1*p1 + x2*p2 +... Here comes the question – how to multiply and then add them?
There are only 2 rows, that’s how much I need :
I’ve enabled only 2 rows, that’s how much I must have for this program:
Const allowedRows As Integer = 2
Private Sub DataGridView1_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserAddedRow
DataGridView1.AllowUserToAddRows = DataGridView1.RowCount <= allowedRows
End Sub
Private Sub DataGridView1_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserDeletedRow
DataGridView1.AllowUserToAddRows = DataGridView1.RowCount <= allowedRows
End Sub
The loop is:
For j = 0 To DataGridView1.Columns.Count
For i As Integer = 0 To DataGridView1.RowCount - 1
multi = CInt(DataGridView1.Rows(i).Cells(j).Value) * CInt(DataGridView1.Rows(i).Cells(j).Value)
Next
EX += multi
Next
I don’t think it must be like this (it gives OutOfRange Exception), but that’s why I’m writing here, so if anyone knows how to do it I’ll be very thankful. Thanks in advance.
I’m more familiar with C Sharp, so forgive my syntax if it’s not 100%.
I feel you’re most the way there. I’ll give both sets of code so you can base it off the C Sharp if my VB syntax is a bit off… either way, the concept should be sound.
VB
C#