Results 1 to 6 of 6

Thread: Structure cannot be indexed because it has no default value

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    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.

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

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    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.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: Structure cannot be indexed because it has no default value

    try this:

    first declare the structure + bindinglist:

    vb Code:
    1. <System.Serializable()> _
    2. Private Structure EmployeeData
    3.     Dim EmployeeName As String
    4.     Dim JobTitle As String
    5.     Dim TaxRate As Single
    6.     Public Overrides Function ToString() As String
    7.         Return EmployeeName
    8.     End Function
    9. End Structure
    10.  
    11. Private EmployeeList As New System.ComponentModel.BindingList(Of EmployeeData)

    then load the bindinglist:

    vb Code:
    1. EmployeeList.Add(New EmployeeData With {.EmployeeName = "john", .JobTitle = "vocals", .TaxRate = 17.5})
    2. EmployeeList.Add(New EmployeeData With {.EmployeeName = "paul", .JobTitle = "bass/vocals", .TaxRate = 17.5})
    3. EmployeeList.Add(New EmployeeData With {.EmployeeName = "george", .JobTitle = "guitar", .TaxRate = 17.5})
    4. EmployeeList.Add(New EmployeeData With {.EmployeeName = "ringo", .JobTitle = "drums", .TaxRate = 17.5})

    serialize (save) the bindinglist:

    vb Code:
    1. Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    2. Dim fs As New IO.FileStream("test.bin", IO.FileMode.Create)
    3. formatter.Serialize(fs, EmployeeList)
    4. fs.Close()

    then to deserialize (read):

    vb Code:
    1. EmployeeList = New System.ComponentModel.BindingList(Of EmployeeData)
    2. formatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    3. Dim fs As New IO.FileStream("test.bin", IO.FileMode.Open)
    4. EmployeeList = DirectCast(formatter.Deserialize(fs), System.ComponentModel.BindingList(Of EmployeeData))
    5. fs.Close()
    6.  
    7. ListBox1.DataSource = EmployeeList

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    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"?

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    14

    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
  •  



Click Here to Expand Forum to Full Width