Variable Array as member of a class

New to FreeBASIC? Post your questions here.
Post Reply
linuxanddos
Posts: 14
Joined: Oct 28, 2023 13:31

Variable Array as member of a class

Post by linuxanddos »

How could I declare an variable sized array as a member of a class?

Sadly, the documentation of FreeBasic is missing explanations and examples with classes and type, as in this part: https://www.freebasic.net/wiki/ProPgArrays

I've tried the following (abc should be a variable sized array) - but it does not work:

Code: Select all

type SomeThing extends Object
	'[...]
end type

type SomeClass extends Object
	declare abstract sub RedimAbcArray(size as Short)
	private:
		as SomeThing abc() 
end type
The error message is:

Code: Select all

error 9: Expected expression, found ')' in 'as SomeThing abc()'
'
If I try to add a dim before the line with the error, this does not solve the problem.
SARG
Posts: 1768
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Variable Array as member of a class

Post by SARG »

Not sure it solves but try adding any.

Code: Select all

as SomeThing abc(any) 
fxm
Moderator
Posts: 12133
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Variable Array as member of a class

Post by fxm »

linuxanddos wrote: Nov 08, 2023 6:50 How could I declare an variable sized array as a member of a class?

Sadly, the documentation of FreeBasic is missing explanations and examples with classes and type, as in this part: https://www.freebasic.net/wiki/ProPgArrays

See TYPE (UDT) at documentation page:
Redim arrayname(array dimensions) As DataType

fieldname(array dimensions) As DataType [= initializer]
fieldname(Any [, Any...]) As DataType

As DataType fieldname(array dimensions) [= initializer], ...
As DataType fieldname(Any [, Any...])
linuxanddos
Posts: 14
Joined: Oct 28, 2023 13:31

Re: Variable Array as member of a class

Post by linuxanddos »

Now it works. It would be cool if someone could document it under the already mentioned link...
linuxanddos
Posts: 14
Joined: Oct 28, 2023 13:31

Re: Variable Array as member of a class

Post by linuxanddos »

Sorry, I missed the post about the UDT page.
nehakakar
Posts: 7
Joined: Jul 04, 2023 6:42
Location: India
Contact:

Re: Variable Array as member of a class

Post by nehakakar »

You can also use through dynamic array with allocate and deallocate methods.

Code: Select all

type AnyThing extends Object
    '[...]
end type

type AnyClass extends Object
    private:
        dim as AnyThing ptr abc()
        declare sub Array(size as Short)
end type

sub AnyClass.Array(size as Short)
    deallocate(abc)
    allocate(abc, size * sizeof(SomeThing))
end sub
Post Reply