Hello all,
I'm new to FB. I was wondering if anybody can tell me how I might be able to dynamically re-dim an array within a user defined type.
I'm trying to use Redim Preserve, but it doesn't remember the new size of the array every time I do this.
ReDim an array within a type
-
- Posts: 2
- Joined: Mar 21, 2008 0:11
- Location: MI, USA
- Contact:
from http://www.freebasic.net/wiki/wikka.php ... KeyPgRedim
allocate a new array for each UDT instance
edit: there is a how-to in the wiki
http://www.freebasic.net/wiki/wikka.php ... aArrayType
I think you can give the UDT an array pointer, and thenRedim cannot be used on arrays contained in UDTs (user-defined Types), because currently only fixed-size arrays are supported in UDTs.
allocate a new array for each UDT instance
edit: there is a how-to in the wiki
http://www.freebasic.net/wiki/wikka.php ... aArrayType
-
- Posts: 2
- Joined: Mar 21, 2008 0:11
- Location: MI, USA
- Contact:
Re:
Today 7 MArch 2021 in help Freebasic say tht there is support but do not give any example , all is a mistery ....calstover wrote:from http://www.freebasic.net/wiki/wikka.php ... KeyPgRedim
I think you can give the UDT an array pointer, and thenRedim cannot be used on arrays contained in UDTs (user-defined Types), because currently only fixed-size arrays are supported in UDTs.
allocate a new array for each UDT instance
edit: there is a how-to in the wiki
http://www.freebasic.net/wiki/wikka.php ... aArrayType
Type Declares a user-defined type.
Syntax
Type typename
fieldname1 As DataType
fieldname2 As DataType
As DataType fieldname3, fieldname4
...
End Type
Type typename [Extends base_typename] [Field = alignment]
[Private:|Public:|Protected:]
Declare Sub|Function|Constructor|Destructor|Property|Operator ...
Static variablename As DataType
fieldname As DataType [= initializer]
fieldname(array dimensions) As DataType [= initializer]
fieldname(Any [, Any...]) As DataType
fieldname : bits As DataType [= initializer]
As DataType fieldname [= initializer], ...
As DataType fieldname(array dimensions) [= initializer], ...
As DataType fieldname(Any [, Any...]) <<<<< =================== DINAMIC ARRAY WITHOUT ANY EXAMPLE
As DataType fieldname : bits [= initializer], ...
Union
fieldname As DataType
Type
fieldname As DataType
...
End Type
...
End Union
...
End Type
How I can redim now ?
Type dat
As UByte Vec1 (Any)
As UByte Vec2 (Any)
End Type
dim some(1 to 20) as dat
redim some (1).Vec1 (1 to 10)
Give Error duplicate definition in some()
Re: ReDim an array within a type
See the Redim documentation page:
redim (some(1).Vec1)(1 to 10)
.....In some cases, it is necessary to specify parentheses around the expression (especially if the array expression itself contains parentheses) - see the examples below.....
- (the last example)
redim (some(1).Vec1)(1 to 10)
Re: ReDim an array within a type
Yes! Thanks a Lot!fxm wrote:See the Redim documentation page:.....In some cases, it is necessary to specify parentheses around the expression (especially if the array expression itself contains parentheses) - see the examples below.....For your code:
- (the last example)
redim (some(1).Vec1)(1 to 10)
Re: ReDim an array within a type
Thanks again and I could to to "Share" the vector 'some' too.fxm wrote:See the Redim documentation page:.....In some cases, it is necessary to specify parentheses around the expression (especially if the array expression itself contains parentheses) - see the examples below.....For your code:
- (the last example)
redim (some(1).Vec1)(1 to 10)
Type dat
As UByte Vec1 (Any)
As UByte Vec2 (Any)
End Type
Dim shared as dat some (1 to 20)
redim (some(1).Vec1)(1 to 10)
Now I can Share a dynamic vector too.....because of Type...
Re: ReDim an array within a type
Or:
Or:
Code: Select all
Type v
As Ubyte u1
As Ubyte u2
End Type
Type dat
As v Vec(Any)
End Type
Dim Shared As dat some(1 to 20)
Redim (some(1).Vec)(1 to 10)
Print some(1).Vec(1).u1
Print some(1).Vec(1).u2
Code: Select all
Type v
As Ubyte u(1 To 2)
End Type
Type dat
As v Vec(Any)
End Type
Dim Shared As dat some(1 to 20)
Redim (some(1).Vec)(1 to 10)
Print some(1).Vec(1).u(1)
Print some(1).Vec(1).u(2)
- This last syntax allows to access whatever element by using index variables (i, j, k) only: some(i).Vec(j).u(k)