ReDim an array within a type

General FreeBASIC programming questions.
Post Reply
CoderLaureate
Posts: 2
Joined: Mar 21, 2008 0:11
Location: MI, USA
Contact:

ReDim an array within a type

Post by CoderLaureate »

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.
calstover
Posts: 68
Joined: Aug 21, 2006 16:51

Post by calstover »

from http://www.freebasic.net/wiki/wikka.php ... KeyPgRedim
Redim cannot be used on arrays contained in UDTs (user-defined Types), because currently only fixed-size arrays are supported in UDTs.
I think you can give the UDT an array pointer, and then
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
CoderLaureate
Posts: 2
Joined: Mar 21, 2008 0:11
Location: MI, USA
Contact:

Post by CoderLaureate »

Excellent.

Thanks for your quick answers.

-jim
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re:

Post by jmgbsas »

calstover wrote:from http://www.freebasic.net/wiki/wikka.php ... KeyPgRedim
Redim cannot be used on arrays contained in UDTs (user-defined Types), because currently only fixed-size arrays are supported in UDTs.
I think you can give the UDT an array pointer, and then
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
Today 7 MArch 2021 in help Freebasic say tht there is support but do not give any example , all is a mistery ....
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()
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ReDim an array within a type

Post by fxm »

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.....
  • (the last example)
For your code:
redim (some(1).Vec1)(1 to 10)
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: ReDim an array within a type

Post by jmgbsas »

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.....
  • (the last example)
For your code:
redim (some(1).Vec1)(1 to 10)
Yes! Thanks a Lot!
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: ReDim an array within a type

Post by jmgbsas »

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.....
  • (the last example)
For your code:
redim (some(1).Vec1)(1 to 10)
Thanks again and I could to to "Share" the vector 'some' too.

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...
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: ReDim an array within a type

Post by fxm »

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
Or:

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)
Post Reply