Where Do I Put Code to Create Classes?

I have tweaked the OOP Classes page to make this clear. Sorry for the confusion.

XojoScript/IDE Scripting is separate from coding in the Xojo IDE. You can see example projects for how this can be used here:

  • Examples/Advanced/XojoScript
  • Examples/Advanced/IDE Scripting

Xojo is its own beast
You dont edit text files directly like you do in so many IDE’s where whatever you type in becomes the class definitions, etc.
You use the IDE they way its presented to you - or you figure out how to work around that and use some other text editor to type in code then incorporate that into you Xojo project through the mechanisms it provides

You can run something like the following as an IDE Script to create a class and add properties

DoCommand "NewClass" PropertyValue("Class1.Name") = "MyClass" DoCommand "NewProperty" ChangeDeclaration("UserName", "Bob Roberts", "String", 2, "")

You could also auto-generate these IDE Scripts to make generating classes easier

after some months you will no more hate the documentation :wink:

I think maybe a point that needs to be stated more clearly is, that in all the Xojo documentation (not just what applies to Classes), there are numerous code examples that make the Xojo language look like a fully “written” language.

For example, code like the following:

Class Vehicle Property Brand As String Property Model As String End Class
Does appear at first glance to imply that you have to “write” all that text to create a class, but in fact it is only a textual representation of how a Class is defined in the Xojo IDE. Having just the text like this is simply a shortcut way (in the documentation, forum postings and blog articles) of showing you code logic without having to also include screen-shots of the graphical view we get from the IDE.

A good example I was just reading yesterday from this page Arrays.Sort(Optional sortMethod As Delegate), shows the following code in one block:

[code]Function DateCompare(value1 As Date, value2 As Date) As Integer
// This assumes the array is populated with non-Nil dates
If value1.SecondsFrom1970 > value2.SecondsFrom1970 Then Return 1
If value1.SecondsFrom1970 < value2.SecondsFrom1970 Then Return -1
Return 0
End Function

Var myArray() As Date
myArray.Add(New Date)
myArray.Add(New Date(2015, 8, 1))
myArray.Add(New Date(2014, 4, 1))
myArray.Add(New Date(2016, 11, 1))

myArray.Sort(AddressOf DateCompare)
// The array is now sorted[/code]
Unfortunately it may not be obvious to the newly initiated to Xojo, that I don’t actually write the Function / End Function keywords to implement the DateCompare method for my Delegate. I have to manually Insert the Method using the IDE Insert feature.

When I first came to Xojo years ago (from a programming background where I wrote all my code in a huge long text editor window), I was at first confused how to implement the code examples I was seeing in the documentation into the IDE. But at some point it became obvious (a aha moment for me, either from a stray comment in a forum post, or something I read somewhere else) that what I was seeing was just “shortcut text” to represent what is graphically represented or achieved in the IDE.

And yes, fortunately you can just copy-and-paste “some” of these text examples into the IDE navigator to implement them, but I don’t think that is the main purpose behind showing the code examples as text.

As a suggestion @Paul Lefebvre , if it doesn’t exist already, maybe we could have a place in the documentation that explains “how to interpret code examples in the documentation”? This page shows a fairly good example of how Methods are represented, both with screen-shots and as simple text, but maybe there needs to be section that explains more explicitly the relationship between the text examples and the IDE features?

Just a thought. I hope that helps.

[quote=492387:@Paul Lefebvre]XojoScript/IDE Scripting is separate from coding in the Xojo IDE. You can see example projects for how this can be used here:

Examples/Advanced/XojoScript
Examples/Advanced/IDE Scripting[/quote]

Thanks, Paul, but none of those provide any examples of how to create a class.
Could you please add a complete examples of creating of:

  1. Creating a class with no superclass
  2. Creating a class with a Xojo superclass (like Control)
  3. Creating Properties and Properties and Methods for the above.

@Brock Nash has provided some simple examples of using IDE script, but when I go to the Xojo document IDE Scripting DoCommand it only provides the DoCommands, and NOT the parameters needed for each. Where is the full documentation for DoCommands?

[h]From DoCommands Page[/h]

[quote]Project Items
NewClass: Add a new class to the current project.

Project Items Editing
AddEventImplementation: Displays the Add Event Handler dialog.
NewMethod: Adds a new method to the selected project item.
NewProperty: Adds a new property to the selected project item.[/quote]

[quote=492390:@Brock Nash]You can run something like the following as an IDE Script to create a class and add properties

DoCommand “NewClass”
PropertyValue(“Class1.Name”) = “MyClass”
DoCommand “NewProperty”
ChangeDeclaration(“UserName”, “Bob Roberts”, “String”, 2, “”)
You could also auto-generate these IDE Scripts to make generating classes easier[/quote]

Thanks, Brock. That at least gives me some hope, but I still don’t understand the script parameters, and how to associate a “NewProperty” with a class I have just created.
None of this is explained in the Xojo documents, or I can’t find it.

Project > Insert > Class

Project > Insert > Class
Select it and in the inspector wSuper put “Control”

SElect the class
Right click and select whatever it is you want to insert
Or Project > Insert > and select what you want to insert
There are short cuts for many

There’s no direct way to just type & create a class, with or without super, ot the methods
Thats just not how things work

Hi, In my former life I had to write specification 1st and then use a UML tool to create classes on the fly.

But sometimes its better to code something in a scrapbook, and when you know what you really want gen specifications out of it…

Sometimes its good to create everything with a powerful IDE and sometimes just to code it , but mostly it ends up by a copy and paste, and the bigger it gets , the more confusion is there…

BR Rainer

[quote=492326:@Jim Underwood]What I really want is to have a custom class library that I can maintain in a file, and use in all of my Projects.

[/quote]
It sounds like you just want to use the “Make external” contextual menu to share a class or module between projects?

https://documentation.xojo.com/getting_started/using_the_ide/project_types.html#External_Items

Bingo! Exactly that!

That would be very helpful.

@Paul Lefebvre , may I also suggest that you add to this opening paragraph of OOP Classes:

Please add:

[quote=492412:@jim mckay]It sounds like you just want to use the “Make external” contextual menu to share a class or module between projects?

https://documentation.xojo.com/getting_started/using_the_ide/project_types.html#External_Items [/quote]

Thanks for the reference. Yes, that looks like something I could use.
I recently discovered the “Folder” container in a Project, and have put my custom classes and Module in it.
So this provides a convenient way to hide (collapse) all of the Folder contents, as well as make external.

Yes. This caused me a lot of the same confusion when I was new to Xojo that Jim Underwood experienced.

@Jim Underwood , I disagree that typing class definitions is faster or better than the Xojo way; for me it’s what makes Xojo a superior programming environment. You cannot end up with a huge page of spaghetti code full of all sorts of methods, class definitions, and so forth, as you can in Swift and other languages - Xojo enforces compartmentalization and organization.

Well guys, here’s proof that it CAN be done – that you can create classes from code.

[h]IDE Script to Create Class[/h]

[code]
Var LF As String = Chr(10)

// — Create and Select a New Class —
DoCommand “NewClass”
PropertyValue(“Class1.Name”) = “MyClass1”

//— Create First Property Since Class is already selected —

//— Scope —
// 0: Public
// 1: Protected
// 2: Private

DoCommand “NewProperty”
// Prop. Name Default Type Scope ???
ChangeDeclaration(“Prop11”, “PUBLIC”, “String”, 0, “”)

//— For Each Additional Property, Must Reselect Class —

If SelectProjectItem(“MyClass1”) Then
DoCommand “NewProperty”
ChangeDeclaration(“Prop12”, “PROTECTED”, “String”, 1, “”)
End If

If SelectProjectItem(“MyClass1”) Then
DoCommand “NewProperty”
ChangeDeclaration(“Prop13”, “PRIVATE”, “String”, 2, “”)
End If

//— Create Method —
If SelectProjectItem(“MyClass1”) Then
DoCommand(“NewMethod”)
ChangeDeclaration(“myMethod11”, “x As Integer, y As Integer”, “Boolean”, 0, “”)
End If

//— Compose the Method Text —
Var methodStr As String
methodStr = “” _

  • LF + “//— Added from IDE Script —” _
  • LF _
  • LF + “Var z As Boolean” _
  • LF + “z = (x > y)” _
  • LF + “Return z”

//— Set the Method Text in the IDE —
Text = methodStr[/code]

[h]Results[/h]

I’m sure some of you will look at this script and think “that’s way too much trouble to use”. If so, then you don’t understand how some programmers work. Here’s a clue: I would never actually type out all of that code.

If you don’t understand the benefit of looking at class, property, and method definitions in code, well you can keep using the “easy” way of point and click. I find lots of value in understanding the classes by viewing in code. And much of it is repetitive, so copy/paste works well. Plus I have tools like Typinator and Keyboard Maestro that can highly automate this kind of stuff.

When I first started designing apps a hundred years ago, I was told by one of my mentors that you should always design your apps to work well with both the mouse, and keyboard only. Some of us prefer to keep our hands on the keyboard as much as possible, because we find that’s most productive for us.

I hope I have helped some future new Xojo user who, like me, has been pulling his/her hair out trying to figure out how to create classes from code. And also maybe I have shown a few of you old hands another way to do things. Choose the way that works best for you.

well done jim, thats going to help so many person.

[quote=492435:@Jim Underwood]Well guys, here’s proof that it CAN be done – that you can create classes from code.
[/quote]

The neat way this can be used is to actually auto-generate classes from other languages or libraries. For example in C# you have reflection which is like introspection. So you could use this to help generate classes to interface between the two or wrap an existing API Library into Xojo. Similarly for other languages :smiley:

[quote=492435:@Jim Underwood]Well guys, here’s proof that it CAN be done – that you can create classes from code.

[/quote]

There was never any doubt that it could be done (I have done it in a different way), just that there was no specific support for it built in.

Another approach is to write a small app to take your text and write the class in Xojo text or Xojo XML file format that can then be imported (via menu or drag and drop) into the IDE.

I have done that occasionally in the past using exported classes/modules etc as templates (of course one needs a license to do that).

That said I prefer to use the IDE with Autocomplete, and have only done that for extremely repetitive stuff, that for me has been very few and far between.

-Karen

Building code to build code from other sources has been something tts been around for ages
I’ve been revisiting code I originally wrote in 2005 before XML document and introspection existed
Its still posted on my site

Once I’m happy(ier?) with the revised code I’ll post it on my git hub as well

But its not “just edit the code to write a class” like Jim asked about

There you go again, Norm, misquoting me. I did NOT say anything about EDIT. For Pete’s sake – just read the topic title.

Jim,
Clearly, you can do this with a text file, as you’ve demonstrated, but I’m puzzled at what the ultimate goal is. At some point after you’ve gone to the trouble of making a script to create classes, properties, methods, windows and controls, you’ll have to execute the script to create your actual project, and from then on, you’ll be working in the IDE. Is it worth this effort just to land in the same place that everyone else is in already?

Or, is the intent to keep the working project permanently as a text file, and all development done using the text file? And whenever you want to test your changes, you run the script and rebuild the entire project from scratch?