variable does not increment - arrays

Public Class Form2
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click
ReDim Preserve Names(0 To i)
Names(i) = txtPatientName.Text
ReDim Preserve Heights(0 To i)
Heights(i) = txtPatientHeight.Text
ReDim Preserve Weights(0 To i)
Weights(i) = txtPatientWeight.Text
i = i + 1
End Sub
End Class
This is my code for when I click a button to enter data into three arrays but when I click the button the i does not increment. Also if I don't enter anything into the Height or Weight textbox and press the button I get the error: Conversion from string "" to type 'Integer' is not valid.
What is the problem here?
Thanks

Related

How to add and convert text to double from a textbox to an array?

I am attempting to write code for a simple calculator that will store up to 10 results in an array.
I have the calculator portion working correctly but I am having troubles storing and displaying the results from the array.
Public Class wk2_David_Thieme_vb
Inherits System.Web.UI.Page
Dim resultArray(9) As Double
Dim i As Integer = 0
Protected Sub btnStore_Click(sender As Object, e As EventArgs) Handles btnStore.Click
Dim Num As Double
If i < 9 Then
For i = 0 To resultArray.Length - 1
Num = Convert.ToDouble(txtResult.Text)
resultArray(i) = Num
i += 1
Next i
Else
txtResult.Text = "10 results already stored"
End If
End Sub
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
For i = 0 To UBound(resultArray)
lstResults.Items.Add(resultArray(i).ToString())
Next
End Sub
The results display 10 zeros in the listbox, no matter how what the user inputs into the calculator. Also, it doesn't display the "10 results already stored" once 10 entries have been added to the array. Thanks for any help you can provide, I am new to vb.net and am still learning, so please be gentle.
I make sample of using List ( of T) to store, display and clear :
Dim ListDoube As New List(Of Double)
Private Sub btnStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStore.Click
Dim Num As Double = 0
If ListDoube.Count < 10 Then
ListDoube.Add(If(Double.TryParse(txtResult.Text, Num), Num, 0))
Else
txtResult.Text = "10 results already stored"
End If
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
For Each myVal In ListDoube
lstResults.Items.Add(myVal.ToString)
Next
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
lstResults.Items.Clear()
End Sub
If you will still use array:
Dim resultArray(9) As Double
Dim i As Integer = 0
Private Sub btnStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStore.Click
Dim Num As Double = 0
If i < 10 Then
resultArray(i) = If(Double.TryParse(txtResult.Text, Num), Num, 0)
Else
txtResult.Text = "10 results already stored"
End If
i += 1
End Sub
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
For Each myVal In resultArray
lstResults.Items.Add(myVal.ToString)
Next
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
For myCnt = 0 To 9
resultArray(myCnt) = 0
Next
lstResults.Items.Clear()
i = 0
txtResult.Text = ""
End Sub
I figured it out, mostly. The only issue now is that the results will always have a zero at the end of the list... not sure how to fix it. So if I input 2 entries, say 4+2 = 6 & 2+2 = 4, then the result array will have 6, 4, 0 in it. Any ideas how to fix this? Again, thanks for the support/help.
Public Shared resultArray(9) As Double
Public Shared i As Integer = 0
Protected Sub btnStore_Click(sender As Object, e As EventArgs) Handles btnStore.Click
If i >= 9 Then
lblError.Text = "10 results already stored"
Else
lblError.Text = ""
resultArray(i) = Convert.ToDouble(txtResult.Text)
If i < 9 Then
i += 1
End If
End If
End Sub
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
If i <= 0 Then
lblError.Text = "You must add numbers"
Else
lstResults.Items.Clear()
lblError.Text = ""
For Num = 0 To i
lstResults.Items.Add(resultArray(Num))
Next Num
End If
End Sub
You don't need any For loop inside btnSave_Click. What a for loop does in your case is that it goes through all of items in your list and adds input number to your array. Instead you need to add each number separately to its own place in array.
To do that just change btnSave like below:
Dim doubleArray(9) As Double
Dim i As Integer = 0
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim Num As Double
If i < 10 Then
doubleArray(i) = (If(Double.TryParse(txtResult.Text, Num), Num, 0))
i += 1
Else
txtResult.Text = "10 results already stored"
End If
End Sub
That way there is nothing wrong inside your btnDisplay_Click.
You can instead use a List to save results in it. Therefore you've to declare a List(Of Double) instead of double array. So btnSave_Click should be changed as below:
Dim listOfDouble As New List(Of Double)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Num As Double
If listOfDouble.Count < 10 Then
listOfDouble.Add(If(Double.TryParse(txtResult.Text, Num), Num, 0))
Else
txtResult.Text = "10 results already stored"
End If
End Sub
And the you can iterate your list using For Each loop as:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
For Each doubleVal In listOfDouble
listResults.Items.Add(doubleVal)
Next
End Sub

Assign multidimensional array to DataGridView in VB.NET

I want to assign 2D array to DataGridView.
There are 2 buttons, the first is an array button which will add my inputs to the array every time I press it.
The second button is a submit button that will assign all of the values of the array to the DataGridView.
But I just can't work it out, every time I press the array button, the value is replaced by the new value I inputted.
Here's the code:
Public Class Form2
Dim array(1, 4) As String
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
For i = 0 To array.GetUpperBound(0)
DataGridView1.Rows.Add(array(i, 0), array(i, 1), array(i, 2), array(i, 3))
Next
End Sub
Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
ReDim array(1, 2)
Dim id, name As String
id = txtID.Text
name = txtName.Text
For i = 0 To array.GetUpperBound(0)
array(i, 0) = id
array(i, 1) = name
Next
End Sub
End Class
This code works for me!
Public Class Form2
Dim arrayCopy(10, 1) As String
Dim b As Integer
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
DataGridView1.Rows.Clear()
For i = 0 To b - 1
DataGridView1.Rows.Add(arrayCopy(i, 0), arrayCopy(i, 1))
Next
End Sub
Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
Dim id, name As String
id = txtId.Text
name = txtName.Text
arrayCopy(b, 0) = id
arrayCopy(b, 1) = name
b += 1
End Sub
End Class

Issue with Array - NullReferenceException was unhandled - VB.NET

I am basically trying to create an array to export checked items onto a word document. But I am getting an error saying
"Object reference not set to an instance of object."
and
"Referenced 'SelectedMutualFunds' has a value of 'Nothing'
Below is my code:
Public Class ExportFunds
Public SelectedMutualFunds() As String
Private Sub ExportFundOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportFundOkButton.Click
Dim i As Integer
Dim array_Counter As Integer
array_Counter = 0
For i = 0 To ExportFundCheckedListBox.Items.Count() - 1
If ExportFundCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
SelectedMutualFunds(array_Counter) = ExportFundCheckedListBox.Items(i).ToString
array_Counter += 1
End If
Next
Me.Close()
End Sub
Can someone please help me solve this issue?
You need to provide length to your string array
Public SelectedMutualFunds() As String
to the following within ExportFundOkButton_Click before you use, preferably just before the for loop.
Redim SelectedMutualFunds(ExportFundCheckedListBox.Items.Count() - 1)
You can get this down to a one-liner and fix the NullReference exception at the same time:
Private Sub ExportFundOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportFundOkButton.Click
SelectedMutualFunds = ExportFundCheckedListBox.Items.Where(Function(i) i.CheckState = CheckState.Checked).Select(Function(i) i.ToString()).ToArray()
Me.Close()
End Sub
Or, slightly longer but easier to read:
Private Sub ExportFundOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportFundOkButton.Click
SelectedMutualFunds = ExportFundCheckedListBox.Items.
Where(Function(i) i.CheckState = CheckState.Checked).
Select(Function(i) i.ToString()).
ToArray()
Me.Close()
End Sub

Using listbox to access 2D array VB

So I am trying to use a listbox to access variables in my 2D array. I am unsure what is the best way to do this. Right now I am using the selectedindex of the listbox to access it but I am only seeing the second dimension being show in my message box. Any help would be appreciated.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
lstInventory.Items.Add("Hand Grenade")
lstInventory.Items.Add("9mm Ammo Box")
lstInventory.Items.Add(".40 Ammo Box")
lstInventory.SelectedIndex = 0
End Sub
Dim dblInventoryItem(,) As Double = {{10.99, 5},
{5.99, 10},
{8.99, 8}}
Private Sub btnCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnCheck.Click
Dim intRow As Integer = lstInventory.SelectedIndex
MessageBox.Show(dblInventoryItem(intRow, 1).ToString)
End Sub
End Class
You don't want to use a multi-dimensional array here.
The OOP way to do this would be to define inventory item as its own class or structure, and use instances to both populate your list box and store the inventory item price and quantity.
Something like:
Public Class Form1
Structure InventoryItem
Public Sub New(ByVal itmName As String, ByVal itmPrice As Double, ByVal itmQty As Integer)
Name = itmName : Price = itmName : Quantity = itmQty
End Sub
Dim Name As String
Dim Price As Double
Dim Quantity As Integer
End Structure
Dim invItems As New List(Of InventoryItem)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
invItems.Add(New InventoryItem("Hand Grenade", 10.99, 5))
'' ... Add your additional items here
For Each i As InventoryItem In invItems
lstInventory.Items.Add(i.Name)
Next
End Sub
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
Dim invItem As InventoryItem = invItems(lstInventoryItems.SelectedIndex)
MessageBox.Show(invItem.Name & "," & invItem.Price & "," & invItem.Quantity)
End Sub
End Class

How to modify a value in the array in visual basic

The following is the code which I am using for creating a two dimensional array
Public Class frmGrades
Private Score As Dictionary(Of String, String) = New Dictionary(Of String, String)
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
Static intNumber As Integer
ReDim Preserve Score(1, intNumber)
Score(0, intNumber) = txtStudent.Text
Score(1, intNumber) = txtGrade.Text
hsbStudent.Maximum = intNumber
hsbStudent.Value = intNumber
intNumber = intNumber + 1
txtGrade.Clear()
txtStudent.Clear()
txtStudent.Focus()
End Sub
Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
txtStudent.Text = Score(0, hsbStudent.Value)
txtGrade.Text = Score(1, hsbStudent.Value)
End Sub
Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
txtStudent.Text = Score(0, hsbStudent.Minimum)
txtGrade.Text = Score(1, hsbStudent.Minimum)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
txtStudent.Text = Score(0, hsbStudent.Maximum)
txtGrade.Text = Score(1, hsbStudent.Maximum)
End Sub
**Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
If Score.ContainsKey(txtStudent.Text) Then
Score.Item(txtStudent.Text) = txtGrade.Text
Else
Score.Add(txtStudent.Text, txtGrade.Text)
End If
End Sub
End Class**
Now I would like to edit the grade text box which should also change the grade in the array. Any idea on how could this be done.
UPDATE:
OK, I see the code has a little more now and the problem is more clear as well as the need for a different approach. I rewrote your code. See if this works for you:
Public Class frmGrades
Public Class StudentGrade
Public Name As String
Public Grade As String
End Class
Private Score As List(Of StudentGrade) = New List(Of StudentGrade)
Private Sub cmdApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApply.Click
AddStudent()
End Sub
Private Sub hsbStudent_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hsbStudent.Scroll
Update(hsbStudent.Value - 1) 'lists are zero-based
End Sub
Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
Update(0)
End Sub
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
Update(hsbStudent.Maximum - 1)
End Sub
Private Sub AddStudent()
Dim nm As New StudentGrade
nm.Name = txtStudent.Text
nm.Grade = txtGrade.Text
Score.Add(nm)
hsbStudent.Minimum = 1
hsbStudent.Maximum = Score.Count
hsbStudent.Value = Score.Count
txtGrade.Clear()
txtStudent.Clear()
txtStudent.Focus()
End Sub
Private Sub Update(i As Integer)
txtStudent.Text = Score(i).Name
txtGrade.Text = Score(i).Grade
End Sub
Private Sub CmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEdit.Click
Dim index As Integer = -1, cnt As Integer = 0
For Each nm As StudentGrade In Score
If nm.Name.ToLower = txtStudent.Text.ToLower Then
index = cnt
Exit For
End If
cnt += 1
Next
If index = -1 Then
AddStudent()
Else
Score(index).Name = txtStudent.Text
Score(index).Grade = txtGrade.Text
hsbStudent.Value = index + 1
End If
End Sub
End Class
With this code you can expand the class StudentGrade to contain whatever you need to store. The list, contrary to dictionary, allow you to use index values as well.

Resources