Index was outside the bounds of the array in vb.net while reading a text file - vb.net

I have a piece of text that has a list of Oscar movies from 1928 - 2014. I'm using an array to find the name of the film and place it into one textbox and the genre in the next. The genre is next to the movies name. I'm trying to access the two separately and place it into two different textboxes.
My Code:
Dim mvs(86) As Movie
Private Sub FrmAcademy_Load() Handles MyBase.Load
Dim mviLine As String
Dim mviData(1) As String
Dim allMovies() As String = IO.File.ReadAllLines("Oscars.txt")
For mviList As Integer = 0 To 86
mviLine = allMovies(mviList)
mviData = mviLine.Split(","c)
mvs(mviList).mviName = mviData(0)
mvs(mviList).gnrName = mviData(1)
Dim mviYear As Integer = 0
Dim mviIndex As Integer = 0
If (IsNumeric(txtAwards.Text)) = True Then
mviYear = CInt(txtAwards.Text)
End If
If (mviYear >= 1928) Or (mviYear <= 2014) Then
mviIndex = 1928 - mviYear
txtFilm.Text = mvs(mviIndex).mviName
txtGenre.Text = mvs(mviIndex).mviName
End If
Next
End Sub
Private Sub btnPicture_Click(sender As Object, e As EventArgs) Handles btnPicture.Click
If (IsNumeric(txtAwards.Text)) = False Then
MessageBox.Show("Please enter a year as a numeric value")
ElseIf CInt(txtAwards.Text) > 2014 Then
MessageBox.Show("Please choose a year in range")
ElseIf CInt(txtAwards.Text) < 1928 Then
MessageBox.Show("Please choose a year in range")
End If

Isn't it supposed to be:
If (mviYear >= 1928) And (mviYear <= 2014) Then
mviIndex = mviYear - 1928

To get better help, at least take the time to format your code properly.
Your mvs array is declared, but where is each element initialized?
On the first line of your For loop, add this code: mvs(mviList) = New Movie()

Related

Show decimal type in listbox VB2008

I have problem in my project with VB 2008.
I have 5 textbox that only contains numeric. From all of inputted value will be shown in Listbox and sort by ascending.
I use this code below
Private Sub InptBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InptBtn.Click
Dim List As New List(Of Double)
Dim chkList As New Integer
Dim getData() As String = {Inpt1.Text, Inpt2.Text, Inpt3.Text, Inpt4.Text, Inpt5.Text, Inpt6.Text}
' empty check
For idx As Integer = 0 To getData.Length - 1
chkList += getData(idx).Trim.Length
Next
If chkList = 0 Then
MessageBox.Show("EMPTY!", "WARNING!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
' numeric check
Dim d1 As Double = 0
For idx As Integer = 0 To getData.Length - 1
If Double.TryParse(getData(idx), d1) Then
List.Add(d1)
End If
Next
If List.Count = 0 Then
MessageBox.Show("Please fill with number!", "WARNING!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
List.Sort()
ListBox.DataSource = List
End Sub
It's work! But I have 1 problem.
If I try to input
0.0000000000001 or 0.00000000000000000002
It's shown like
1E-13 or 2E-20
I tried to use ToString method and change List type by String after sorting like code below, but it doesn't work.
For idx As Integer = 0 To getData.Length - 1
If Double.TryParse(getData(idx), d1) Then
List.Add(d1.ToString())
End If
Next
Could I show all inputted value as I inputted with Decimal type? Or there's some way to shown it with other type?
Shown like below in Listbox:
0.0000000000001 or 0.00000000000000000002
Thank you so much!
Can you try this ?
For idx As Integer = 0 To getData.Length - 1
If Double.TryParse(getData(idx), d1) Then
List.Add(FormatNumber(d1.ToString, 20))
End If
Next

FInd a title by searching with only a portion of the text?

Title says it all, I need help making my search input box find the title and display it when the user only types part of the movie title before searching. This is what I have now and it works great, but you must type in the complete title. Any help would be appreciated! Thanks
Private Sub btnSearch_Click(sender As Object, e As EventArgs)Handles btnSearch.Click
'Searches for movie in listbox
Dim strDVDtitle As String
strDVDtitle = InputBox("Enter Movie Title:")
Dim X As Integer = 0
Dim bolDVDFound As Boolean = False
For X = 0 To count - 1
If DVDS(X).DVDtitle = strDVDtitle Then
txtDVDyear.Text = DVDS(X).DVDyear
txtDVDtitle.Text = DVDS(X).DVDtitle
txtDVDyear.Text = DVDS(X).DVDyear
txtDVDruntime.Text = DVDS(X).DVDruntime
txtDVDrating.Text = DVDS(X).DVDrating
bolDVDFound = True
End If
Next
If bolDVDFound = False Then
MessageBox.Show("Movie not found")
End If
End Sub
You can use the contains string method, like this:
Dim actualTitle = "The Martian"
If actualTitle.ToLower().Contains(strDVDtitle.ToLower()) Then
MsgBox("Match!")
End If

Sorted List in Array Visual Basic [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I have a program that creates a list of 25 random numbers from 0 to 1,000. I have to buttons the first button will load a list box with the random numbers and the second button will sort the list of numbers from the smallest to largest which is where I implemented bubble sort code. Now the other list box that is supposed to hold the sorted numbers doesn't work properly it only shows one number instead of all of them.
Here is my code:
Option Strict On
Public Class Form1
Dim rn As Random = New Random
Dim Clicked As Long = 0
Dim numbers, sort As Long
Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
Clicked += 1
For x = 0 To 25
numbers = rn.Next(0, 1000)
RandomBox.Items.Add(numbers)
If Clicked >= 2 Then
RandomBox.Items.Clear()
Clicked = 1
End If
Next
End Sub
Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
Dim Sorted() As Long = {numbers}
Dim Swapped As Boolean
Dim endOfArray As Integer = Sorted.Length - 1
Dim Tmp As Byte
While (Swapped)
Swapped = False
For I = 0 To endOfArray - 1
If Sorted(I) > Sorted(I + 1) Then
Tmp = CByte(Sorted(I))
Sorted(I) = Sorted(I + 1)
Sorted(I + 1) = Tmp
Swapped = True
End If
endOfArray = endOfArray - 1
Next
End While
SortBox.Items.Clear()
For I = 0 To Sorted.Count - 1
SortBox.Items.Add(Sorted(I))
Next
End Sub
End Class
Change your:
Dim Sorted() As Long = {numbers}
to
Sorted(x) = numbers
edit: Since you changed your code. You need to put back in the line that loads the Sorted Array.
For x = 0 To 25
numbers = rn.Next(0, 1000)
RandomBox.Items.Add(numbers)
Sorted(x) = numbers
If Clicked >= 2 Then
RandomBox.Items.Clear()
Clicked = 1
End If
Next
and remove the:
Dim Sorted() As Long = {numbers}
from the second part and put this declaration back in the beginning like you had:
Dim Sorted(26) as Long
The way you have will only show the latest random number. It is not any array but a single entity. Therefore only the latest will be add into the array. You need to load each number into the array as you create each one. Thus the (x) which loads it into position x.
You didn't use any arrays at all in your project...you're using the ListBox as your storage medium and that's a really bad practice.
I recommend you set it up like this instead:
Public Class Form1
Private rn As New Random
Private numbers(24) As Integer ' 0 to 24 = 25 length
Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
For x As Integer = 0 To numbers.Length - 1
numbers(x) = rn.Next(0, 1000)
Next
' reset the listbox datasource to view the random numbers
RandomBox.DataSource = Nothing
RandomBox.DataSource = numbers
' empty out the sorted listbox
SortBox.DataSource = Nothing
End Sub
Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
' make a COPY of the original array that we will sort:
Dim sorted(numbers.Length - 1) As Integer
Array.Copy(numbers, sorted, numbers.Length)
Dim Swapped As Boolean = True
Dim endOfArray As Integer = Sorted.Length - 1
Dim Tmp As Integer
While (Swapped)
Swapped = False
For I As Integer = 0 To endOfArray - 1
If sorted(I) > sorted(I + 1) Then
Tmp = sorted(I)
sorted(I) = sorted(I + 1)
sorted(I + 1) = Tmp
Swapped = True
End If
Next
endOfArray = endOfArray - 1
End While
' reset the listbox datasource to view the sorted numbers
SortBox.DataSource = Nothing
SortBox.DataSource = sorted
End Sub
End Class
Also, note that you were decrementing endOfArray inside your for loop. You should only decrement it after each pass; so outside the for loop, but inside the while loop.
Additionally, you were using a Tmp variable of type Byte, but generating numbers between 0 and 999 (inclusive). The Byte type can only hold values between 0 and 255 (inclusive).
Your Bubble Sort implementation was very close to correct!

Visual Basic: How can i display the prime numbers between 1 and the inputted number

Hello everyone so i'm trying to find the prime numbers of any input. I want them to be in a listbox and the input in a text box. I would like to use two arguments but i don't know how to. this is the code i have i need dire help. I am not the best at visual basic i just need some guidance. My code isn't working but display a drop down box when i press display.
Public Class Form1
Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
Dim prim As Integer
Dim test As Integer
Dim imPrime As Boolean = False
prim = CInt(txtNum.Text)
test = prim
If prim = 1 Then
imPrime = False
MessageBox.Show("Enter a number greater than one please")
Else
Do While prim >= 2
For i As Integer = 2 To prim
If prim Mod i = 0 Then
imPrime = False
Exit For
Else
imPrime = True
lstPrime.Items.Add(prim)
End If
Next
Loop
End If
If imPrime = True Then
lstPrime.Items.Add(prim)
End If
End Sub
End Class
This is my fastest VBA code to generate prime numbers between two numbers.
The generated prime numbers are put in clipboard. You will need to open
your ms office word and type Ctrl+V to view all the generated prime numbers.
Sub generateprimenumbersbetween()
Dim starting_number As Long
Dim last_number As Long
Dim primenumbers As Variant
Dim a As Long
Dim b As Long
Dim c As Long
starting_number = 1 'input value here
last_number = 1000000 'input value here
primenumbers = ""
For a = starting_number To last_number
c = Round(Sqr(a)) + 1
For b = 2 To c
If a = 1 Or (a Mod b = 0 And c <> b) Then
Exit For
Else
If b = c Then
primenumbers = primenumbers & " " & a
Exit For
End If
End If
Next b
Next a
Dim answer As DataObject
Set answer = New DataObject
answer.SetText primenumbers
answer.PutInClipboard
End Sub
I think the while loop is not working as you intend. You need two loops, the first one counting up to the possible prime, and an inner one counting up to the counter in the outer loop.
You can find examples everywhere... here's one implemented in C#, but since your question was specifically about a listbox, I've translated it to VB.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
calculatePrimes()
End Sub
Private Sub calculatePrimes()
Dim prim As Integer
Dim count As Integer = 0
prim = CInt(Me.TextBox1.Text)
If prim < 3 Then
MsgBox("Please enter a bigger number")
Return
End If
Me.ListBox1.Items.Clear()
For i As Integer = 1 To prim
Dim isPrime As Boolean = True
For j As Integer = 2 To i
If (i Mod j <> 0) Then count = count + 1
Next
If count = (i - 2) Then Me.ListBox1.Items.Add(i)
count = 0
Next
End Sub
End Class
(This assumes you have a textbox for input called TextBox1 and a listbox for display called ListBox1)

Visual Basic Confusion

I have been required to create a program that asks me to find the maximum value of one particular array. I am using multiple forms in this project and have used a user-defined data type and created multiple array under it. There is a first form that is related to this, which defines my defined data type is gStudentRecord and the arrays that define it are last name, Id, and GPA. This second form is where I write all of the code to display what I want. My question is how to get the Max GPA out of that array. I'm sorry if this isn't in very good format, this is the first time I've used Stackoverflow
Public Class frmSecond
Private Sub frmSecond_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Ctr As Integer
Dim Line As String
lstDisplay.Items.Clear()
lstDisplay.Items.Add("Name".PadRight(25) & "ID".PadRight(16) & "GPA".PadRight(20) & "Out of state".PadRight(10))
For Ctr = 0 To gIndex Step 1
Line = gCourseRoster(Ctr).LastName.PadRight(20) & gCourseRoster(Ctr).ID.PadRight(15) & gCourseRoster(Ctr).GPA.ToString.PadRight(15) & gCourseRoster(Ctr).OutOfState.ToString().PadLeft(5)
lstDisplay.Items.Add(Line)
Next
End Sub
Private Sub btnStats_Click(sender As Object, e As EventArgs) Handles btnStats.Click
Dim Ctr As Integer = 0
Dim Average As Double
Dim Sum As Double
Dim Found As Boolean = False
Dim Pass As Integer
Dim Index As Integer
lstDisplay.Items.Clear()
**For Ctr = 0 To gIndex Step 1
If gCourseRoster(Ctr).GPA > gCourseRoster(Ctr).GPA Then
lstDisplay.Items.Add(gCourseRoster(Ctr).GPA)
End If
Next**
Average = gComputeAverage(Sum)
lstDisplay.Items.Add("Number of Students: " & gNumberOfStudents)
lstDisplay.Items.Add("Average: " & Average)
End Sub
Private Function gComputeAverage(Sum As Double) As Double
Dim Ctr As Integer
Dim Average As Double
For Ctr = 0 To gIndex Step 1
Sum = Sum + gCourseRoster(Ctr).GPA
Next
Average = Sum / gNumberOfStudents
Return Average
End Function
End Class
You can use a Lambda expression to tease it out. The Cast part is converting from the gCourseRoster to a collection of Double by supplying the GPA to the Select statement.
Dim gList As New List(Of gCourseRoster)
gList.Add(New gCourseRoster With {.id = 1, .name = "Bob", .GPA = 3.9})
gList.Add(New gCourseRoster With {.id = 2, .name = "Sarah", .GPA = 3.2})
gList.Add(New gCourseRoster With {.id = 3, .name = "Frank", .GPA = 3.1})
Dim maxGPA = gList.Cast(Of gCourseRoster).Select(Function(c) c.GPA).ToList.Max
MessageBox.Show(maxGPA.ToString)
Output: 3.9