Hi everyone
I have coded the following class then when i m trying to compile i m receiving the following error message (in the FillList procedure at the following line : LstCooking.Items.Add(ck))
"ArgumentNullException was Unhandled "
Please help
Thanks and regards
Armand
Imports System.IO
Imports System.IO.File
Public Class Form1
Private ReadOnly mFilePath As String = "..\..\cooking.txt"
Private allcookings() As Cooking
Private Sub Form1_Load() Handles MyBase.Load
GetFile()
FillList()
End Sub
Private Sub FillList()
LstCooking.Items.Clear()
For Each ck As Cooking In allcookings
LstCooking.Items.Add(ck) <--------------
Next
End Sub
Public Function GetFile() As Boolean
' Read the data file, try to find the account ID.
' If found, read the account name and balance and
' return True. If not found, return false.
' An array is used to hold the workshops, since
'not all students may
' be familiar with collections.
Dim infile As StreamReader = Nothing
LblResult.Text = String.Empty
Try
infile = OpenText(mFilePath)
Dim numVals As Integer = CInt(infile.ReadLine)
ReDim allcookings(numVals - 1)
For i As Integer = 0 To numVals - 1
Dim entireLine As String = infile.ReadLine()
Dim fields() As String = entireLine.Split("\"c)
Dim cook As New Cooking
cook.mcategory = CType(fields(0), Cooking.CookingType)
cook.CookingTime = CInt(fields(1))
cook.mcost = CDbl(fields(2))
cook.mtitle = fields(3)
allcookings(i) = cook
Next
Return True
Catch ex As Exception
LblResult.Text = ex.Message
Return False
Finally
If infile IsNot Nothing Then
infile.Close()
End If
End Try
Return False
End Function
Private Sub btnDetails_Click() Handles BtnDetails.Click
Dim index As Integer = LstCooking.SelectedIndex
If index = -1 Then
LblResult.Text = "Please select a cooking"
Return
Else
LblResult.Text = String.Empty
End If
Dim frm As New DetailsForm
frm.onecooking = allcookings(index)
frm.ShowDialog()
FillList()
End Sub
End Class
PS
The arrow was added just to locate the line that s raising the error message
Once again thanks for any help
Loading
VulpesPosted May 9, 2012, 10:11 AM
If you change this line:
cook.mcategory = CType(fields(0), Cooking.CookingType)
to this:
cook.mcategory = CType(fields(0), Cooking.CookingType) - 1
then I think it will fix it.
balla etoyo ignace yvesPosted May 9, 2012, 1:00 PM
I have modified according to your last suggestion
The problem is SOLVED it works perfectly
MANY THANKS , REGARDS FOR YOUR PATIENCE, SUGGESTIONS AND KINDNESS
balla etoyo ignace yvesPosted May 9, 2012, 9:44 AM
the modification was done but the same error message remains
i dont understand why ?
VulpesPosted May 9, 2012, 8:25 AM
Hopefully, changing the array declaration as follows will fix it:
Public Shared CookingCategory() As String = {"Cookies",
balla etoyo ignace yvesPosted May 9, 2012, 7:27 AM
Many thanks for you help
I have tried your suggestion related with ToString function : it results with the following error message
IndexOutOfRangeException was unhandled by user code
Index outside the bounds of the arrays( the arrow pointed at ",cost=" )
PS
Thanks and regards
VulpesPosted May 9, 2012, 5:14 AM
balla etoyo ignace yvesPosted May 8, 2012, 10:01 PM
i have made all the last changes you ve suggested I m having the following error
Any suggestion from you will be helpfull
invalid cast exception was raised in the To string function
Error message
InvalidCastException was unhandled by user code
Conversion from string "3 ,cost =15,00 €" to type 'Integer' is not valid.
Public Overrides Function ToString() As String
Return mtitle & "," & CookingTime & ",category =" & CookingCategory(mcategory & " ,cost =" & mcost.ToString("c"))
End Function
balla etoyo ignace yvesPosted May 8, 2012, 7:54 PM
thanks for the suggestion
i have correct line inside the txt file
and remove spaces between lines now i ll add the code you ve suggested and compile again the program
i ll let you know the result in a while
once again many thanks for your time suggestion and availability
regards
VulpesPosted May 4, 2012, 7:10 PM
Firstly, this line:
2\10\8 crêpes
is missing a '\' after the 8.
Secondly, you need to allow in your code for the large number of blank lines. I suggest the following alteration in the GetFile method:
balla etoyo ignace yvesPosted May 4, 2012, 3:15 PM
Following is the contain of cooking.txt file ( 19 rows)
19
3\45 \15\chaussons au miel
3\30\10\biscuits viennois
1\60\5\ganache
5\60\12\tarte aux pommes
1\25\17\beignets
2\10\8 crêpes
4\8\10\gauffres
5\75\120\bûche au chocolat
6\120\75\confiture de fraises
6\30\25\crême brulée
6\10\17\tiramisu
6\20\14\cheese cake
2\30\22\soufflé au chocolat
2\60\50\galette des rois
2\55\60\kougelhof
1\75\100\muffins
1\50\70\madeleines
6\20\20\glace au chocolat
5\45\30\gateau à l'orange
VulpesPosted May 4, 2012, 10:13 AM
Can you post the first two or three lines.
balla etoyo ignace yvesPosted May 4, 2012, 9:26 AM
i havent solve the problem yet
regards
balla etoyo ignace yvesPosted May 4, 2012, 6:24 AM
But unfortunately the listbox is still empty and nothing is load at the execution
i think the To string function should look like this ( something similar)
I m trying i have found the working function (with suitable terms)
public overrides Function ToString() as string
return mTitle & ",Days" =" & Numdays & ",Category =" &
CategoryNames(category & " ,cost =" & cost.tostring("c")
End function
Many thanks for your suggestions
VulpesPosted May 3, 2012, 7:59 PM
balla etoyo ignace yvesPosted May 3, 2012, 4:29 PM
I appreciate your contribution
In fact it needs to call the Tostring() Function of the cooking time class
which i presume i did not wrote correctly. I m annoyed about how to rewrite it.
Many thanks for the suggestions
Armand
Public Class Cooking
Enum CookingType
cookies
gateaux
vienoiserie
biscuits
patisserie
dessert
End Enum
Public Shared CookingCategory() As String = {"cookies",
"Gateaux", "Viennoiserie", "Biscuits", "Dessert"}
Public Property mtitle As String
Public Property CookingTime As Integer
Public Property mcost As Double
Public Property mcategory As cookingtype
Public Overrides Function ToString() As String
Return mtitle & CookingTime & mcategory & mcost.ToString("c")
End Function
End Class
VulpesPosted May 3, 2012, 11:50 AM
If you haven't set it then I think it will try to call Cooking's ToString() method, if it has one, and fill the ListBox with the results of that.
You could also specify a property, say mtitle, directly:
balla etoyo ignace yvesPosted May 3, 2012, 11:23 AM
I have made the change you ve suggested. The application compile but display nothing in the Listbox
I think there something wrong with this bunch of code the GETFILE function
Try
infile = OpenText(mFilePath)
Dim numVals As Integer = CInt(infile.ReadLine)
ReDim allcookings(numVals - 1)
For i As Integer = 0 To numVals - 1
Dim entireLine As String = infile.ReadLine()
Dim fields() As String = entireLine.Split("\"c)
Dim cook As New Cooking
cook.mcategory = CType(fields(0), Cooking.CookingType)
cook.CookingTime = CInt(fields(1))
cook.mcost = CDbl(fields(2))
cook.mtitle = fields(3)
allcookings(i) = cook
Next
Return True
Catch ex As Exception
LblResult.Text = ex.Message
Return False
Finally
If infile IsNot Nothing Then
infile.Close()
End If
End Try
Nothing is added to Allcooking i m trying to solve the matter, i havent find yet
Once again thanks for your suggestion
Regards
VulpesPosted May 3, 2012, 10:28 AM