PDA

View Full Version : Need help assigning class to arrays



kryton9
10-05-2017, 05:26
I can't figure out how to assign a class to an array of that class type.
Error Given:9691


Here is sample code to see what I mean. Thanks


Uses "Console"

Type cVec
Public
x As Single
y As Single
z As Single
Private
vec2 As Boolean ' is this a Vec2 or Vec3, default vec2 is false, auto set when created

Public
Function _Create ( Optional x As Single, Optional y As Single, Optional z As Single )
Me.vec2 = FALSE
If x = "" Then
Me.x = 0
Me.y = 0
Me.z = 0
Return
End If
Me.x = x
Me.y = y
If z = "" Then
z = 0
Me.vec2 = TRUE
End If
Me.z = z
End Function

Function cpyFrm ( copyFromVec As cVec )
Me.x = copyFromVec.x
Me.y = copyFromVec.y
Me.z = copyFromVec.z
End Function
End Type

Type cParticle
pos As cVec
vel As cVec
acc As cVec
prevPos As cVec
maxSpeed As Single

Function _Create ( )
Dim pos As cVec( Rnd ( width ), Rnd ( height ) )
Dim vel As cVec 0, 0
Dim acc As cVec 0, 0
Dim prevPos As cVec 0, 0
prevPos.cpyFrm pos
Single maxSpeed = 1
End Function
End Type

Int32 width = 640
Int32 height = 480
Int32 numPoints = 10
Dim particles ( numPoints ) As cParticle

Local i As Int32
For i = 1 To numPoints
particles( i ) = New cParticle '<<<<< how should I do this, getting an error, thanks
'particles( i ) = New cParticle() '<<<<< how should I do this, getting an error, thanks
Next

PrintL "Press a key to end program"
WaitKey

Michael Hartlef
10-05-2017, 06:51
As you know, I can't test right now. But maybe, with the DIM statement the array is already defined and assigned.

What error do you get?

kryton9
10-05-2017, 07:48
Here is a screenshot of the error. Actually I will add it to the first post to keep it together. Thanks Mike.

http://www.thinbasic.com/community/showthread.php?12765-Need-help-assigning-class-to-arrays&p=93523&viewfull=1#post93523

Michael Hartlef
10-05-2017, 08:47
Are Private and Public valid keywords? The editor doesn't highlight them.

ErosOlmi
10-05-2017, 09:46
Hi,

thinBasic OOP is quite at the beginning and not all is really correct as it should be, so you must be patient and maybe we will go together into the right direction.
Especially arrays of UDTs is an area not really covered.

Anyway, here some hints

Public, Private are keywords just parsed but internally they just do nothing. They only mark UDT elements with an internal flag public/private for future usage.

When "Dim particles ( numPoints ) As cParticle" is encountered, the cParticle._Create contructor is automatically called.
If you expect constructor is executed when you invoke the NEW keywork, at the moment it is not for arrays of UDT. But this can be changed.

So, at the moment, better to have contructor and destructor only as GLOBAL function to the UDT and add some specialized function.

To test how many optional parameters has been passed, there is Function_CParams function.


You code can be changed into something like that to get what I said. Hope this can help.


Uses "Console"
Uses "console"

Type cVec
Public
x As Single
y As Single
z As Single
Private
vec2 As Boolean ' is this a Vec2 or Vec3, default vec2 is false, auto set when created

Public

Function Init ( Optional x As Single, Optional y As Single, Optional z As Single )
Static conta As Long
Incr conta
PrintL $TAB, Function_Name, conta


Me.vec2 = FALSE


If Function_CParams = 0 Then'x = "" Then
Me.x = 0
Me.y = 0
Me.z = 0
Return
End If


Me.x = x
Me.y = y
If z = "" Then
z = 0
Me.vec2 = TRUE
End If
Me.z = z


End Function

Function cpyFrm ( copyFromVec As cVec )
Me.x = copyFromVec.x
Me.y = copyFromVec.y
Me.z = copyFromVec.z
End Function


End Type


Type cParticle
pos As cVec
vel As cVec
acc As cVec
prevPos As cVec
maxSpeed As Single

Function SetParticle ( )
Static conta As Long
Incr conta
PrintL Function_Name, conta


'Dim pos As cVec( Rnd ( width ), Rnd ( height ) )
'Dim vel As cVec 0, 0
'Dim acc As cVec 0, 0
'Dim prevPos As cVec 0, 0
Me.pos.Init( Rnd ( width ), Rnd ( height ) )
Me.prevPos.cpyFrm(Me.pos)
Me.maxSpeed = 1

End Function
End Type


Int32 width = 640
Int32 height = 480
Int32 numPoints = 10
Dim particles ( numPoints ) As cParticle


Local i As Int32
For i = 1 To numPoints
' particles( i ) = New cParticle '<<<<< how should I do this, getting an error, thanks
'particles( i ) = New cParticle() '<<<<< how should I do this, getting an error, thanks
Particles(i).SetParticle()
PrintL i, particles(i).pos.x
Next


PrintL "Press a key to end program"
WaitKey

kryton9
10-05-2017, 11:42
Helps a lot, thank you very much Eros