Results 1 to 5 of 5

Thread: fixed array in a structure

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    2

    fixed array in a structure

    Hi,

    Im looking into VB.net for a project but I have come against a problem. Structures required in some DLLs have arrays of byte such as

    Type WSAData
    A(100) As Byte
    B As Integer
    End Type

    The problem is I cant find out how i can replicate this structure in VB.net since you cant use Type definition but have to use Structure, in which you can only define a reference to a dynamic array.

    Is there any way arround this?

    thanks

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok I've tried your code but gives this msg :

    Arrays declared as structure members cannot be declared with an initial sizeSee Also
    Arrays | Structure Declaration
    An array in a structure is declared with an initial size.

    To correct this error

    Define arrays in structures as dynamic.
    You can re-dimension dynamic arrays with if a fixed-size array is required. For example:
    VB Code:
    1. Structure MyStruct
    2.    Public MyArray() As Integer
    3. End Structure
    4.  
    5. Sub UseStruct()
    6.    Dim Struct As MyStruct
    7.    ReDim Struct.MyArray(9) ' Dimension as 10 elements
    8.    Struct.MyArray(2) = 777 ' Use the array.
    9. End Sub
    according to MS.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    2
    Thanks, but thats no use. Thats a dynamic array where I need a static fixed array.. the problem is thus:

    VB Code:
    1. Structure MyStruct
    2.    Public MyArray() As Integer
    3.    Public MyInt As Integer
    4. End Structure
    5.  
    6. Sub UseStruct()
    7.    Dim Struct As MyStruct
    8.    ReDim Struct.MyArray(9) ' Dimension as 10 elements
    9.    Struct.MyArray(2) = 777 ' Use the array.
    10. End Sub

    would in fact produce something like the following behind the sceens:
    Pointer MyArray (this uses up 4 bytes of data)
    Integer MyInt (this uses up 4 bytes of data)

    Somewhere else in memory a block of 10 bytes of memory is allocated and the variable MyArray references it


    In contrast
    VB Code:
    1. Type MyStruct
    2.    MyArray(9) As Integer
    3.    MyInt As Integer
    4. End Type

    would produce something like the following behind the sceens:
    Integer *10 MyArray (this uses up 4*10 bytes of data = 40)
    Integer MyInt (this uses up 4 bytes of data)

    So as you can see there is a big difference in terms of where the data is stored in memory, and the DLL im interfacing with will only accept the data in the format produced by the Type definition.

    Thanks for any more help on this

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If I'm not wrong , you're trying to create new user-data type.Well,I've never faced such this stuff.

  5. #5
    Member
    Join Date
    Mar 2002
    Posts
    56
    I know this is a little old, but:

    Structure Whatever
    <vbfixedarray (15)> public ArrayName as Integer
    End Structure

    Defines a fixed array of 16 integers.

    For strings, use

    <vbfixedstring(15)> public StringName as String

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