Results 1 to 2 of 2

Thread: Problem passing structure array to another function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Problem passing structure array to another function

    Hello, all, wondering if someone can tell me what I'm doing wrong here. I'm simply trying to pass a structure array to another function for some evaluation. The problem is, it hits an error when I try to pass it. I'm using VB.NET 2003. Here is my code below.


    VB Code:
    1. Private Structure Fields
    2. Dim FieldName As String
    3. Dim FieldValue As String
    4. End Structure
    5. 'In declarations section
    6.  
    7. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    8.  
    9.  
    10. Dim ArrValidate() As Fields
    11. Dim cntrl As Control
    12. Dim X As Long
    13.  
    14. 'Fill my array
    15. For Each cntrl In Me.Controls
    16. X = X + 1
    17. ReDim Preserve ArrValidate(X)
    18. ArrValidate(X).FieldName = cntrl.Name
    19. ArrValidate(X).FieldValue = cntrl.Text
    20. Next cntrl
    21.  
    22. 'Pass the array to my function located in another class
    23. Validation.Validate_frmProducts(ArrValidate) 'Dies here
    24.  
    25. end sub

    Error I get is:
    Z:\HTTP\ProductMgmtApp\frmProducts.vb(576): Value of type 1-dimensional 'array of ManagementApp.frmProducts.Fields' cannot be converted to '1-dimensional array of System.Object' because 'ManagementApp.frmProducts.Fields' is not a reference type.


    Anyone know what I'm doing wrong? Thanks in advance

    Strick

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem passing structure array to another function

    The error message is fairly explicit. The method argument is an Object array and you are passing a Fields array. There is no valid conversion between the two because Fields is a structure and therefore a value type. You will either need to make Fields a class so it is a reference type and the conversion is therefore valid, or else put your Fields objects in an Object array instead of a Fields array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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