Import Class Code from Text File

I feel stupid like I should know this but I can’t figure it out.

How do you import an entire class that you have in a text file into Xojo? When I try importing it, I just get an alias to the file. Is there some extra formatting or something that needs to be used?

Just so I am clear - how do you import something like this where you can look at an example project code on Christian’s site:

https://www.monkeybreadsoftware.net/example-audio-portaudio.shtml

Thanks!

You might need to change the extension on the file if it isn’t what Xojo expects. I don’t know what the correct extension is at the moment but if you export an existing class to a text file you can see what it should be.

Yeah it’s xojo_code but when I import, it’s just the class name and nothing else that gets imported (ie: no properties, methods or anything else).

When it imports, are you getting an empty class (small blue box icon) or a external item (document icon, name in italics)?

Depending on how I do it, I get either one…

Edit: I’m not so sure this post is relevant, text projects don’t allow external .xojo_code items. Perhaps they borked something…

Xojo text format is very sensitive. You may not always be able to copy and paste code directly from external sources and try to trick the IDE by shoving it into a .xojo_code file.

If it’s an important chunk of your own code, you may want to open a text editor and start just moving it piece by piece now. I was able to successfully drag a .xojo_code file written by the IDE into the navigator using 2022r2. You could try using the older version like I do, but I suspect this might be a problem with some assumptions about the text format being more simple than it is.

To answer your question about MBS examples specifically, the best solution for MBS examples is to open the project files from the download package. You can download them separately if necessary.

Yeah, the MBS one was just an example. This was another place where I got code. I’m not trying to use an external item but to import it. If you look at .xojo_code files, there’s all sorts of #Tag lines around every different method, event, etc. It looks like these are needed. It does appear you can’t just import raw code into Xojo.

How would the IDE know where to put it, even if you could “just import it” ?

You had said you’d gotten the name-in-italics item, which is an external item. The IDE should be automatically importing .xojo_code directly and not allowing you to reference it as an external. It may be the internals of the document confused the IDE as to what it was though… that’s just speculation :upside_down_face:

FWIW, it’s more than just the #tags, the nesting and spacing is important too. It’s a bit more sensitive than it looks, one of the reasons ARGen would generate XML projects.

Thanks all. I guess it’s not so simple then… :smiley:

The text file must be like this:

a) class definition (in this example the class is “clsSentinel”)

#tag Class
Protected Class clsSentinel

b) Protected Property definition:

  #tag Property, Flags = &h1
    Protected RS_Sentinel As RecordSet
  #tag EndProperty

c) Public Property definition:

  #tag Property, Flags = &h0
    Snt_ID As Integer
  #tag EndProperty

d) Private Property definition

  #tag Property, Flags = &h21
    Private mRecordFound As Boolean
  #tag EndProperty

e) Computed Property definition:

  #tag ComputedProperty, Flags = &h0
        #tag Getter
            Get
              Return mRecordFound
            End Get
        #tag EndGetter
        RecordFound As Boolean
  #tag EndComputedProperty

f) Private Method definition:

  #tag Method, Flags = &h21
    Private Sub ClearProperties()
    // Inizializzazione proprietà

    Snt_ID = 0
    Snt_FolderName = ""
    Snt_DistFromJobStart = 0
    Snt_RowNumber = 0
    Snt_DateTime = Nil
    Snt_Type = ""

    ' ReDim Record(-1)

    Dict = Nil


    mRecordFound = False

  End Sub
  #tag EndMethod

g) Public Method definition:

  #tag Method, Flags = &h0
    Function Delete() As Boolean
      // Cancellazione record tabella Sentinel

      Dim RS As RecordSet
      Dim SQL As String

      SQL = "DELETE FROM Sentinel WHERE 1 = 1 " 

      SQL = SQL + " AND Snt_ID = " + Cstr(Snt_ID)

      DB_SQLServer.SQLExecute(SQL)

       // errore ritornato dal Database
      If DB_SQLServer.Error Then
        mPropErrMessage = DB_SQLServer.ErrorMessage
        Return False
      Else
        Return True
      End If


    End Function

  #tag EndMethod

h) at the end of the class:


End Class
#tag EndClass

Greetings - Please allow me to chime-in, because I fully understand the problem that Jon is trying to solve. It’s simple: We get some really good code from AI bots, which is perfectly legit, and we want to import it into our Xojo app. A simple copy and paste into the IDE should suffice, but as Jon described, Xojo does not allow it.

Instead of simply copying and pasting a class or module or even a half-dozen methods, Xojo requires us to copy and paste one method at a time… properties are even more tedious. When I use Python or VBA or other languages, the process is simple and straight forward - just paste it in with the rest of the code… their respective IDEs are smart enough to recognize their own code and assimilate it in with existing code. Unfortunately, it is extremely tedious with Xojo. Even with the import option, using a .xojo_code text file, the results are in vain, as Jon described.

If this has been addressed and resolved, then I apologize for my rant - please point me to the solution. This shortcoming is going to become more pronounced as people catch onto AI assistance, and the Xojo audience is going to become smaller if it is not addressed.

Thanks for listening - I will be anxiously watching for replies.
Don

1 Like

Have you tried to drag and drop the external file into the Navigation pane ?

Yeah doesn’t work. It’s been a while since I first opened this thread but I am 99% sure that I tired that.

I just tried dragging and dropping the external file into the Navigation pane and it added an icon with an arrow, like a shortcut symbol, but no code was added to the IDE that can be edited from the IDE.

Jon, based on your reply, I’m guessing that you have not found a solution, is that correct?

This indicates the file was added as an external text resource and not code. (Meaning you’d be able to load it into a String quite easily if you wanted).

As I mentioned several posts up in this very thread, the plain text format is more complicated than it looks.

Yes, I read your previous posts, but I’m really surprised that Xojo does not have a feature to import text code. It seems they could have easily integrated a feature that would programatically take care of the little details, rather than putting it on our plate. It would make our coding experience and lives easier. Importing code should not be a tedious task.

Thanks for your responses - I would love to hear about anything else that you learn about this.

This issue does not have a workaround nor is it fixed. I would suggest that you file a feature request. I’m happy to sign on to it.

@Tim_Parnell is correct in that the plain text format is very complicated.

In my case, I was using AI generated code to try to figure out how to access Google public calendar data (It’s a freaking PUBLIC calendar yet Google makes you jump through an insane amount of security hoops trying to download that data. I gave up. So I’ve not pursued this issue further…

Okay - I submitted a case for a Feature Request: https://tracker.xojo.com/xojoinc/xojo/-/issues/77670, and I referenced this thread.

Please sign on to it to give it some votes!

Thanks!

Done!