Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a function return a custom TYPE?
#1
Question 
Hello! Former PDS 7.1 developer here, and I recently stumbled on some of my old source code files while digging through an old hard drive. That led me to qb64 and eventually here!

So I decided to take a stab at completing a little text-based map scroller that I started a few decades ago, and I'm using some of the new conventions available with QB64PE.

It brings me my first question: Can a function return a custom TYPE? If so, how?

Consider the following 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"
config_item = Split_Header_line(lines$)
' Expecting config_item.Label = "title" and config_item.Value = "Main Entrance"

Function Split_Header_Line (line$)
    Dim 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
    Split_Header_Line = result
END FUNCTION 

I get the following two errors:

Expected = similar user defined type on line 10

and

User defined types in expresssions are invalid on line 21

Is this telling me that I can't use TYPEd variables as return types for functions or am I missing something more obvious?
Reply


Messages In This Thread
Can a function return a custom TYPE? - by 12centuries - 06-12-2024, 10:38 PM



Users browsing this thread: 1 Guest(s)