How to access to an ANY Arrey in a udt??

General FreeBASIC programming questions.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

How to access to an ANY Arrey in a udt??

Post by mrminecrafttnt »

I am just writing on an dynamic Menu engine but i get
Illegal non-static member access, found 'items' in 'print cast(menu_item ptr,menu.items(i).menuitemname)'

Code: Select all

type menu_item
    menuitemname as string
    menuitemadress as sub ptr
    menubutton as ubyte
    declare sub set_menuitem (menutext as string,menu_adress as sub)
end type
    


type menu
    menuname as string
    dim as integer px,py
    declare sub add_item (itemname as string,menu_adress as sub)
    declare sub open_menu
    items(any) as menu_item
    declare constructor
    declare destructor
end type

sub menu.add_item(itemname as string,menu_adress as sub)
    if menu_adress > 0 then
        
        redim preserve items(ubound(items)+1)
        
        with items(ubound(items))
            .menuitemname = itemname
            .menuitemadress = @menu_adress
            if .menubutton = 0 then print "MAX BUTTON REACHED - PLEASE CREATE A SUB MENU TO PREVENT BUGS!!" : EXIT SUB
        end with
    end if
end sub

sub menu.open_menu
    locate px,py
    print "**";menuname;"**"
    print "****************"
    for i as integer = lbound(items) to ubound(items)
        locate px+2+i,py
        print cast(menu_item ptr,menu.items(i).menuitemname)
    next
    
    
end sub

constructor menu
    redim preserve items(ubound(items)+1)
end constructor

destructor menu
    erase(items)
end destructor

sub demo
    print "THIS IS A DEMO"
end sub

sub exit_sub
    end
end sub

dim as menu test
test.menuname = "MAIN MENU"
test.add_item ("TESTMODE",@demo)
test.add_item ("EXIT",@exit_sub)
test.open_menu
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to access to an ANY Arrey in a udt??

Post by dodicat »

Should be this.
instead of menu.

also the cast needs @this.

print this.items(i).menuitemname prints EXIT
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: How to access to an ANY Arrey in a udt??

Post by grindstone »

Code: Select all

Sub menu.open_menu
    Locate px,py
    Print "**";menuname;"**"
    Print "****************"
    For i As Integer = LBound(items) To UBound(items)
        Locate px+2+i,py
        'Print Cast(menu_item Ptr,menu.items(i).menuitemname)
        Print items(i).menuitemname '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Next
   
   
End Sub
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: How to access to an ANY Arrey in a udt??

Post by paul doe »

Code: Select all

type menu_item
    menuitemname as string
    menuitemadress as sub ptr
    menubutton as ubyte
    declare sub set_menuitem (menutext as string,menu_adress as sub)
end type
   


type menu
    menuname as string
    dim as integer px,py
    declare sub add_item (itemname as string,menu_adress as sub)
    declare sub open_menu
    items(any) as menu_item
    declare constructor
    declare destructor
  
  private:
    m_items as integer
end type

sub menu.add_item(itemname as string ,menu_adress as sub)
  if menu_adress > 0 then
    m_items += 1
    redim preserve items( 0 to m_items - 1 )
   
    with items( ubound( items ) )
        .menuitemname = itemname
        .menuitemadress = @menu_adress
    end with
  end if
end sub

sub menu.open_menu
    locate px,py
    print "**";menuname;"**"
    print "****************"
    for i as integer = lbound(items) to ubound(items)
        locate px+2+i,py
        ? items(i).menuitemname
    next
end sub

constructor menu
  redim preserve items(ubound(items)+1)
end constructor

destructor menu
    erase(items)
end destructor

sub demo
    print "THIS IS A DEMO"
end sub

sub exit_sub
    end
end sub

dim as menu test
test.menuname = "MAIN MENU"
test.add_item ("TESTMODE",@demo)
test.add_item ("EXIT",@exit_sub)
test.open_menu

sleep()
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: How to access to an ANY Arrey in a udt??

Post by mrminecrafttnt »

Here we go! I solve this so:

Code: Select all

type menu_item
    menuitemname as string
    menuitemadress as sub
    menuitembutton as ubyte
    declare sub runit
end type
   
type menu
    menuname as string
    dim as integer px,py
    declare sub add_item (itemname as string,menu_adress as sub )
    declare sub open_menu
    items(any) as menu_item
    declare constructor
    declare destructor
end type

sub menu.add_item(itemname as string,menu_adress as sub )
    if menu_adress > 0 then
       
       if ubound(items) <= 8 then
           redim preserve items(ubound(items)+1)
       else
           print "Max Menus Reached" : Exit sub
       end if
       
       
        with items(ubound(items))
            .menuitemname = itemname
            .menuitemadress = menu_adress
            .menuitembutton = ubound(items)
        end with
    end if
end sub

sub menu.open_menu
    locate px,py
    print "*************"
    locate px+1,py
    print "**";menuname;"**"
    locate px+2,py
    print "*************"
    for i as integer = lbound(items)+1 to ubound(items)
        locate px+2+i,py
        with items(i)
            print .menuitembutton;". ";.menuitemname
        end with
        
    next
    print "*************"
    print "Your choiche :";
    dim as integer i = val(input(1))
    print i
    for x as integer = lbound(items) to ubound(items)
        if i = items(x).menuitembutton then
            if items(x).menuitemadress > 0 then items(x).menuitemadress() else print "NULL POINTER IN MENU ENTRY" 'execute sub here
            exit sub
        end if
    next
end sub

constructor menu
    redim preserve items(ubound(items)+1)
end constructor

destructor menu
    erase(items)
end destructor

sub demo
    cls
    print "ADD DEMO HERE"
end sub

sub exit_sub
    cls
    print "EXIT"
    end
end sub

'menu setup
dim as menu test
test.menuname = "MAIN MENU"
test.add_item ("Demo",@demo)
test.add_item ("Exit",@exit_sub)
test.px = 2
test.py = 2

'main loop
do
    test.open_menu
    sleep 1
loop
Post Reply