Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a function return a custom TYPE?
#3
(06-12-2024, 11:16 PM)SMcNeill Wrote: You can't use User Defined Types as the function return itself, but you can use them as a parameter and pass the value back and forth.   

An example with your code:
Code: (Select All)
' Split a "label:value" string in to two parts, stored as a custom TYPE

TYPE config_item_type
    Label AS STRING
    Value AS STRING
END TYPE

DIM config_item AS config_item_type
line$ = "title: Main Entrance"
junk = Split_Header_Line(line$, config_item)
' Expecting config_item.Label = "title" and config_item.Value = "Main Entrance"
PRINT config_item.Label, config_item.Value

Split_2 line$, config_item
PRINT config_item.Label, config_item.Value

FUNCTION Split_Header_Line (line$, result AS config_item_type)
    i% = INSTR(line$, ":")
    IF i% THEN
        result.Label = LCASE$(LEFT$(line$, i%))
        result.Value = LTRIM$(MID$(line$, i% + 1))
    END IF
END FUNCTION

SUB Split_2 (line$, result AS config_item_type)
    i% = INSTR(line$, ":")
    IF i% THEN
        result.Label = LCASE$(LEFT$(line$, i%))
        result.Value = LTRIM$(MID$(line$, i% + 1))
    END IF
END SUB

As you can see, you can pass those values via parameter; just not as a return through the function itself.  (Which might make it just easier to use a SUB, as I've included above as a demo for you, as well. )
I would use the function and return i%
when returned you might want to know if the config_item was changed
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply


Messages In This Thread
RE: Can a function return a custom TYPE? - by mdijkens - 06-13-2024, 12:54 PM



Users browsing this thread: 2 Guest(s)