Results 1 to 6 of 6

Thread: Need help assigning class to arrays

  1. #1
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Need help assigning class to arrays

    I can't figure out how to assign a class to an array of that class type.
    Error Given:ArrayOfClassError.jpg


    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
    
    Last edited by kryton9; 10-05-2017 at 07:49.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  2. #2
    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?

  3. #3
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    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/s...ll=1#post93523
    Last edited by kryton9; 10-05-2017 at 07:50.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  4. #4
    Are Private and Public valid keywords? The editor doesn't highlight them.

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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
    
    Last edited by ErosOlmi; 10-05-2017 at 11:44.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404
    Helps a lot, thank you very much Eros
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Similar Threads

  1. creating my own class
    By ReneMiner in forum thinBasic General
    Replies: 4
    Last Post: 17-11-2012, 20:07
  2. How to run a Dot Net 4.0 class from Powerbasic
    By zak in forum Software discussion
    Replies: 1
    Last Post: 17-12-2011, 11:27
  3. class+object+dialog
    By Lionheart008 in forum UI (User Interface)
    Replies: 3
    Last Post: 17-11-2009, 22:09
  4. Class Libraries using Oxygen
    By Charles Pegge in forum O2h Compiler
    Replies: 3
    Last Post: 10-08-2009, 05:15

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •