Results 1 to 6 of 6

Thread: [RESOLVED] System Argument Exceptions (Out of Range & System Exception)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    3

    Resolved [RESOLVED] System Argument Exceptions (Out of Range & System Exception)

    I'm at a lost for fixing the System.ArgumentOutofRange Exception and System.Argument Exception errors I keep getting. I have a project that requires multiple forms and eventually pulling information from a text file. I've been working this project for about 3 weeks and just can't correct these errors. There is a event handler for one of the forms that requires the following:

    First Half - Everything worked fine

    In the Click event of the btnAddToBasket button, perform the following steps:
    a. Create a GroceryItem object using the values from the controls and add it to the basket variable.
    Note: Remember the basket variable is the GroceryBasket collection.
    b. Verify all controls except txtScanNumber contain a value.
    c. Set the value of the txtScanNumber control using the following code:
    txtScanNumber. Text = _txtBrandName.Text.Subs t ring(0 , 3) & 1 0 1 9
    d. Instantiate the GroceryItem class, using the control values.
    e. Use the following expression to set the Aisle property. The expression converts the text into an Aisle enumeration.
    [Enum].Parse(Get Type(Aisle), cboAisle.Text )
    f. Add the GroceryItem object to the basket variable.
    14. In the Click event of the Exit menu item, end the application.
    15. Have the btnAddToBasket_Click method handle the Click event of AddToolStripMenuItem as well.

    Second Half -
    Open the GroceryItemForm.vb file.
    3. Add the options &Save and &L oad to the Application menu above the Exit option. You can add the Save and
    Exit options to the Application menu underneath Exit and then click and drag them above it.
    4. Add the OpenFileDialog and SaveFileDialog components to the Windows form.
    5. In the Click event for the Save option, prompt the user for a file location and store the basket as a text
    file. Recall that grocery items are stored in the basket variable.
    Hint: You could use a CSV file similar to a flat-file database.
    6. In the Click event for the Load option, prompt the user for a file location and load the contents of the text file
    into the basket. Make sure you empty the basket first.
    Hint: You could use the Split method in the String class to break each line into individual array items.
    7. Save your work and test the application.
    a. Add some items to the basket and save them.
    b. Then attempt to load them again.
    c. Click on the View option in the Basket menu to see the basket contents. You will modify how this menu
    option works in the concluding steps.

    So, I wrote the following code starting with the instructions from the first half and checking the application before writing the code for the second half:
    Imports System.IO

    Public Class GroceryItemForm

    Private Property strFilename As String

    Private Sub GroceryItemForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim GroceryItemForm As New Form()
    GroceryItemForm.Show()
    GroceryItemForm.Hide()
    End Sub

    Private Function ValidateMyControls() As Boolean
    Dim allComboBoxes() As ComboBox = {cboAisle}
    Dim allTextBoxes() As TextBox = {txtScanNumber, txtBrandName}

    Dim emptyTB As TextBox = allTextBoxes.Where(Function(f) f.Text = "").FirstOrDefault
    Dim emptyCB As ComboBox = allComboBoxes.Where(Function(f) f.SelectedIndex = -1).FirstOrDefault
    If emptyTB IsNot Nothing Then
    MessageBox.Show("Please fill value in " & emptyTB.Name)
    Return False
    Else
    'All set to go!
    Return True
    End If
    End Function

    Private Sub btnAddToBasket_Click(sender As Object, e As EventArgs) Handles btnAddToBasket.Click, AddToolStripMenuItem.Click

    Dim objGroceryItem As New GroceryItemForm

    objGroceryItem.lblScanNumber.Text = txtScanNumber.Text = _txtBrandName.Text.Substring(0, 3) & "1019" - System.ArgumentOutofRange Exception objGroceryItem.lblBrandName.Text = "&Name"
    objGroceryItem.lblPrice.Text = "&Price"
    objGroceryItem.lblAisle.Text = [Enum].Parse(GetType(GroceryItem.AisleType), cboAisle.Text) - System.ArgumentException
    objGroceryItem.grpDescription.Text = "&Description"
    objGroceryItem.btnAddToBasket.Text = "A&dd to Basket"

    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
    'End application
    Application.Exit()
    End Sub

    Private Sub ViewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewToolStripMenuItem.Click
    BasketDisplayForm.ShowDialog()
    MessageBox.Show("And is successfully added")
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
    With saveDialog
    .DefaultExt = "txt"
    .FileName = strFilename
    .Filter = "text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
    .FilterIndex = 1
    .OverwritePrompt = True
    .Title = "Save File Dialog"
    End With
    If saveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
    Try
    strFilename = saveDialog.FileName
    My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Text", True)
    Catch ex As Exception
    MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    End If
    End Sub

    Private Sub LoadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadToolStripMenuItem.Click
    With openDialog
    .Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
    .FilterIndex = 1
    .Title = "Save File Dialog"
    End With
    If openDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
    Try
    strFileName = openDialog.FileName
    Catch ex As Exception
    MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    End If
    End Sub

    End Class

    I even added the code to check for empty values, but I'm still getting the same errors. Can anyone provide some insight into correcting these errors?

    Thanks. I've been up since 3 am.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: System Argument Exceptions (Out of Range & System Exception)

    Code:
    objGroceryItem.lblScanNumber.Text = txtScanNumber.Text & _txtBrandName.Text.Substring(0, 3) & "1019"
    what are you trying to achieve here?

    Code:
    objGroceryItem.lblAisle.Text = [Enum].Parse(GetType(GroceryItem.AisleType), cboAisle.Text)

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    3

    Re: System Argument Exceptions (Out of Range & System Exception)

    I have combo box that uses a dropdown list to select the aisle. There are seven different aisles in this project to choose.

  4. #4
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: System Argument Exceptions (Out of Range & System Exception)

    As I understand it, you're getting an "Out Of Range" exception on the following line:

    Code:
    objGroceryItem.lblScanNumber.Text = txtScanNumber.Text = _txtBrandName.Text.Substring(0, 3) & "1019"
    If that's correct, the obvious place to look is at the Substring call... it will generate precisely that exception if the value in _txtBrandName.Text is less than 3 characters long. You don't tell us what the value of that variable is during the run, so we can't judge. But that looks to be the likely culprit for the first of your exceptions.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,394

    Re: System Argument Exceptions (Out of Range & System Exception)

    Quote Originally Posted by Forever Trying View Post
    I have combo box that uses a dropdown list to select the aisle. There are seven different aisles in this project to choose.
    use the [combobox].selectedindex property

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2015
    Posts
    3

    Re: System Argument Exceptions (Out of Range & System Exception)

    Thanks. That what I thought too, but all of the text for the txtBrandName so far have been no less than four characters long. I changed the = sign between txtScanNumber and txtBrandName, as suggested above and haven't gotten the error again. But that pesky [Enum].parse...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width