|
-
Mar 8th, 2010, 03:05 PM
#1
Thread Starter
New Member
Structure cannot be indexed because it has no default value
Hi Guys,
Can anyone tell me what I'm missing here:
Code:
Structure EmployeeData
Dim EmployeeName As String
Dim JobTitle As String
Dim TaxRate As Single
End Structure
Public Sub OpenEmployeeFile(ByVal ViewForm As Object, ByVal ListBox As Object)
Dim EmployeeList As EmployeeData = Nothing
nEmployees = 0
ViewForm.EmployeeListBox.Items.Clear()
FileOpen(1, "EmployeeData.epc", OpenMode.Binary, OpenAccess.Read)
While Not EOF(1)
FileGet(1, EmployeeList)
EmployeeArray(nEmployees) = EmployeeList
ViewForm.ListBox.Items.Add(EmployeeList.EmployeeName)
End While
End Sub
Last edited by ssjaronx4; Mar 8th, 2010 at 03:44 PM.
-
Mar 8th, 2010, 03:46 PM
#2
Re: Structure cannot be indexed because it has no default value
you're using pre .net code there. there are newer more efficient ways to do that.
how are you saving "EmployeeData.epc"? it could be done with binary serialization in a few lines of code + deserialized with another few lines of code.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 8th, 2010, 04:00 PM
#3
Thread Starter
New Member
Re: Structure cannot be indexed because it has no default value
Thanks for the reply. I'm creating a PAYE calculator as uni coursework.
Basically what I want to do is have a file which stores Employee Name, Job Title and tax rate which can be read into the program to fill combo boxes etc.
I'm then going to have another file with Job Title and Paye Rate.
I then want to be able to read them into the program and populate various text, combo, labels and listviews.
For example, my main page has a combo box for selecting the employee. When I select an employee I want to fill in Job title, tax rate and paye rate. Once done I will use the data in those fieilds to calculate monthly paye.
I'm going to have a separate form to edit the two text files.
This is just a uni program so I'm not too worried about people being able to read the files in a text editor etc.
-
Mar 8th, 2010, 04:28 PM
#4
Re: Structure cannot be indexed because it has no default value
try this:
first declare the structure + bindinglist:
vb Code:
<System.Serializable()> _
Private Structure EmployeeData
Dim EmployeeName As String
Dim JobTitle As String
Dim TaxRate As Single
Public Overrides Function ToString() As String
Return EmployeeName
End Function
End Structure
Private EmployeeList As New System.ComponentModel.BindingList(Of EmployeeData)
then load the bindinglist:
vb Code:
EmployeeList.Add(New EmployeeData With {.EmployeeName = "john", .JobTitle = "vocals", .TaxRate = 17.5})
EmployeeList.Add(New EmployeeData With {.EmployeeName = "paul", .JobTitle = "bass/vocals", .TaxRate = 17.5})
EmployeeList.Add(New EmployeeData With {.EmployeeName = "george", .JobTitle = "guitar", .TaxRate = 17.5})
EmployeeList.Add(New EmployeeData With {.EmployeeName = "ringo", .JobTitle = "drums", .TaxRate = 17.5})
serialize (save) the bindinglist:
vb Code:
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New IO.FileStream("test.bin", IO.FileMode.Create)
formatter.Serialize(fs, EmployeeList)
fs.Close()
then to deserialize (read):
vb Code:
EmployeeList = New System.ComponentModel.BindingList(Of EmployeeData)
formatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New IO.FileStream("test.bin", IO.FileMode.Open)
EmployeeList = DirectCast(formatter.Deserialize(fs), System.ComponentModel.BindingList(Of EmployeeData))
fs.Close()
ListBox1.DataSource = EmployeeList
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 11th, 2010, 03:36 PM
#5
Thread Starter
New Member
Re: Structure cannot be indexed because it has no default value
Thanks for the help. With a bit of extra help from my boss at work I'm sort of getting use to using the streamreader. However, back to my initial query, I found that I still cannot use a structure as it keeps telling me I have no defaut structure.
If I use the write code and the structure withiun the call of a form it works. When I try and move the code to a module, I get the above error. Why is it that the above error occurs when I try and use the structure in a module as apposed to a form? Is there something I dont know about the use of structures in a module and how do I give it a "default value"?
-
Mar 11th, 2010, 04:15 PM
#6
Thread Starter
New Member
Re: Structure cannot be indexed because it has no default value
Tell a lie. I just added the stucture and all code to the main form and I still get an error about the default values. Help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|