Where Do I Put Code to Create Classes?

OK, I am brand new to Xojo, and I need some basic help. If the answer already exists in the Forum or Xojo Docs, please provide a link.

This is such a basic question I feel like it should be very obvious in the Xojo documentation. Maybe I have overlooked it, but I have searched everywhere extensively, and can not find instructions on where to put the code to create classes in my Project, and how to I cause that code to be executed.

I expected it on this page, but it is not there:
OOP Classes From Xojo Documentation

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.

I tried creating a Module in my Project with a Method with this code, but it gives me a compile error of “Syntax error End Class”

Class apoRegexResults
  
  // PROPERTIES
  
  Var fullMatchStr as String
  Var captureGroups() As String
  
  Public Sub Test
  return
  End Sub
  
End Class

This seems like a valid class definition based on the above Xojo document.
What am I doing wrong?

You create it in the IDE…
See the “Insert” Icon on the upper right on the toolbar? That drops a menu… Chose Class

You can create a Class at the top level of the navigator or in a module

-Karen

[quote=492327:@Karen Atkocius]You create it in the IDE…
See the “Insert” Icon on the upper right on the toolbar? That drops a menu… Chose Class[/quote]
Thanks for the quick reply, Karen. But I guess you missed my key requirement: CODE to Create Classes.

I know how to create a class in the IDE, but I want to create this class, and many more, purely from code.
So, my question is, where in the IDE do I put my CODE to create the classes, and how do I execute that code?

Karen has given you the answer. You create the classes in the navigator and not in code.

[quote=492328:@Jim Underwood]Thanks for the quick reply, Karen. But I guess you missed my key requirement: CODE to Create Classes.

I know how to create a class in the IDE, but I want to create this class, and many more, purely from code.
So, my question is, where in the IDE do I put my CODE to create the classes, and how do I execute that code?[/quote]

You don’t, you create the class as Karen posted. You add properties and methods to your class by using the IDE. When you need to use the class in code you create an instance of the class: Dim yourVar as NEW YOUR_CLASS

Sorry guys, but I am having a very hard time believing that there is no way to create classes from code, given this Xojo document:
OOP Classes From Xojo Documentation

[quote]Classes
You can create a class using the Class…End Class commands. A new class can have properties, methods, and events. Here is an example of how you can define a class:

// Sample Class definition
Class NewClass
  // Property declarations
  Dim i As Integer
  Dim c As Color
  // Method declarations
  Sub myMethod (x As Integer, y As Integer)
    // Method code goes here
  End Sub
End Class

[/quote]

This seems to be an XY Problem

The same document says:

Its understandable that if you come from a C background (and even from VB at a stretch) ,you expect to have an enormous blank text file into which you can type code such as

[code]Class apoRegexResults

// PROPERTIES

Var fullMatchStr as String
Var captureGroups() As String

Public Sub Test
return
End Sub

End Class[/code]

But in Xojo, the class and its properties have more attributes than the easily visible ones.
if you save the code as a text format, you will find that a class actually looks like this:

[code]
#tag Class
Protected Class Class1
#tag Property, Flags = &h0
nNumber As Integer
#tag EndProperty

#tag Property, Flags = &h0
	szWords As String
#tag EndProperty


#tag ViewBehavior
	#tag ViewProperty
		Name="Name"
		Visible=true
		Group="ID"
		Type="String"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Index"
		Visible=true
		Group="ID"
		InitialValue="-2147483648"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Super"
		Visible=true
		Group="ID"
		Type="String"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Left"
		Visible=true
		Group="Position"
		InitialValue="0"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Top"
		Visible=true
		Group="Position"
		InitialValue="0"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="nNumber"
		Group="Behavior"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="szWords"
		Group="Behavior"
		Type="String"
		EditorType="MultiLineEditor"
	#tag EndViewProperty
#tag EndViewBehavior

End Class
#tag EndClass[/code]

After some experimenting, it is possible to omit the behaviour aspects
So if you are determined to do it as a text file, you could create a text file like this, and import it

[code]#tag Class
Protected Class Class1a
#tag Method, Flags = &h0
Sub Method_1(s as string)
msgbox s
End Sub
#tag EndMethod

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

#tag Property, Flags = &h0
	szWords As String
#tag EndProperty

End Class
#tag EndClass[/code]

[quote=492326:@Jim Underwood]OK, I am brand new to Xojo, and I need some basic help. If the answer already exists in the Forum or Xojo Docs, please provide a link.

This is such a basic question I feel like it should be very obvious in the Xojo documentation. Maybe I have overlooked it, but I have searched everywhere extensively, and can not find instructions on where to put the code to create classes in my Project, and how to I cause that code to be executed.

I expected it on this page, but it is not there:
OOP Classes From Xojo Documentation

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.

I tried creating a Module in my Project with a Method with this code, but it gives me a compile error of “Syntax error End Class”

Class apoRegexResults
  
  // PROPERTIES
  
  Var fullMatchStr as String
  Var captureGroups() As String
  
  Public Sub Test
  return
  End Sub
  
End Class

This seems like a valid class definition based on the above Xojo document.
What am I doing wrong?[/quote]

Copy the Example Code and Paste it into the Navigator. This is no Coding Example, but the Code which is in your Clipboard when you copy a Class. :wink:

And again, you create Classes only in the IDE :slight_smile:

I was going to suggest that, but oddly, pasting into the navigator hasn’t worked for me for a long time.

[quote=492331:@Jim Underwood]Sorry guys, but I am having a very hard time believing that there is no way to create classes from code, given this Xojo document:
OOP Classes From Xojo Documentation[/quote]

Sorry guy, most of us have not read the documentation… we’ve been doing this for years…

Try this link https://documentation.xojo.com/topics/code_management/sharing_code_among_multiple_projects.html

you could export your class and then link it into all of your other projects. (at windows its drag&drop with shift+control key)
as mentioned above you can save a project as type “Xojo Project” which create single files. (instead of binary or xml)
your class file would have the extension .xojo_code then, you could edit with a editor with care.
would be useful if you convert a project from other language.

[quote=492333:@Jeff Tullin]The same document says:

Its understandable that if you come from a C background (and even from VB at a stretch) ,you expect to have an enormous blank text file into which you can type code such as

[code]Class apoRegexResults

// PROPERTIES

Var fullMatchStr as String
Var captureGroups() As String

Public Sub Test
return
End Sub

End Class[/code]

But in Xojo, the class and its properties have more attributes than the easily visible ones.
if you save the code as a text format, you will find that a class actually looks like this:

[code]
#tag Class
Protected Class Class1
#tag Property, Flags = &h0
nNumber As Integer
#tag EndProperty

#tag Property, Flags = &h0
	szWords As String
#tag EndProperty


#tag ViewBehavior
	#tag ViewProperty
		Name="Name"
		Visible=true
		Group="ID"
		Type="String"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Index"
		Visible=true
		Group="ID"
		InitialValue="-2147483648"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Super"
		Visible=true
		Group="ID"
		Type="String"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Left"
		Visible=true
		Group="Position"
		InitialValue="0"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="Top"
		Visible=true
		Group="Position"
		InitialValue="0"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="nNumber"
		Group="Behavior"
		Type="Integer"
	#tag EndViewProperty
	#tag ViewProperty
		Name="szWords"
		Group="Behavior"
		Type="String"
		EditorType="MultiLineEditor"
	#tag EndViewProperty
#tag EndViewBehavior

End Class
#tag EndClass[/code]

After some experimenting, it is possible to omit the behaviour aspects
So if you are determined to do it as a text file, you could create a text file like this, and import it

[code]#tag Class
Protected Class Class1a
#tag Method, Flags = &h0
Sub Method_1(s as string)
msgbox s
End Sub
#tag EndMethod

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

#tag Property, Flags = &h0
	szWords As String
#tag EndProperty

End Class
#tag EndClass[/code][/quote]
Jeff, don’t confuse the text format with the actual structure of a class. They are not the same. [quote=492331:@Jim Underwood]Sorry guys, but I am having a very hard time believing that there is no way to create classes from code, given this Xojo document:
OOP Classes From Xojo Documentation[/quote]
Jim, what the others are saying is true. You cannot create a class in Xojo code. The only place that the Class...End Class pattern works is inside the XojoScript plugin.

Also, If you could point out on that page where you found that bit of text, I’ll get it corrected as I was unable to locate it in a quick search.

[quote=492344:@Greg O’Lone][/quote]
i think what Jim asked is some kind of feature request.
this classic view would also more productive for edit.

https://documentation.xojo.com/topics/xojoscript/introduction_to_xojoscript.html_Language

[quote][h]Classes[/h]
You can create a class using the Class…End Class commands. A new class can have properties, methods, and events. Here is an example of how you can define a class:

// Sample Class definition

Class NewClass // Property declarations Dim i As Integer Dim c As Color // Method declarations Sub myMethod (x As Integer, y As Integer) // Method code goes here End Sub End Class
A class can be subclassed from another class using the Inherits command.[/quote]

I was thinking this might be possible the an “IDE SCRIPT” (File -> IDE Scripts)

There is:

DoCommand "NewProperty"

But I’m not sure how to set the property name and type through the IDE Scripting…

[quote=492344:@Greg O’Lone]Jim, what the others are saying is true. You cannot create a class in Xojo code. The only place that the Class…End Class pattern works is inside the XojoScript plugin.

Also, If you could point out on that page where you found that bit of text, I’ll get it corrected as I was unable to locate it in a quick search.[/quote]

Greg, thanks for jumping in and confirming that we can’t create classes for our project using code. That is very sad, as I’m sure that everyone knows it is much faster to type out class definitions than to use the IDE with multiple mouse point-and-clicks. This is especially true if the classes have no Xojo superclass.

May I suggest a change to your class documentation that you explicitly state that “classes cannot be created using code. They must be created using the IDE.” That would have saved me many hours of research, watching videos, trial-and-error, and of course posting here, wasting other people’s time.

As @Alberto DePoo pointed out, the code creating classes that I quoted came from the XojoScript Language From Xojo Documentation. While I have since learned that you cannot use XojoScript to create classes (or other entities) for direct use in your project, you will note that on that page nowhere does it state that. When I discovered that page I was elated, thinking here is how to create classes via code – but no, that is wrong.

Here’s a quote from the top of that page:

[quote]
IDE Scripting
This topic describes the language operators, data types, keywords and commands that can be used with XojoScript and IDE Scripting.

Note: You cannot use any built-in Xojo classes within XojoScript.

. . .

Classes
You can create a class using the Class…End Class commands. A new class can have properties, methods, and events. Here is an example of how you can define a class:[/quote]

I hope you, and everyone, can see that for someone new to Xojo, that page clearly implies that we can create classes using code.

Having said all that, it does appear that maybe classes for use in the project could be created from code using IDE Scripting. However, there are no examples of doing so given on that page. If it is possible to do so, could I ask you (or someone at Xojo) to provide an example project showing how to use IDE Scripting to create classes?

Thanks.

XojoScript ? Xojo in much the same way JavaScript ? Java
Indeed, I do not recall this being explicitly mentioned anywhere

Apples and oranges. XojoScript is part of Xojo, and is part of Xojo’s documentation.
Nowhere in JavaScript documentation will you find examples of using Java, and vise versa.

Furthermore, there are examples of using XojoScript in your Xojo project to do things like add end-user scriptability.

I don’t know that I agree with this at all.

If I want to make a class, I just drag the Class icon from the Library into the navigator. I’ll probably then rename it, and start adding Methods and Properties. If it’s a class with a Super (such as my two subclasses of SSLSocket), then giving it a Super is pretty easy, too.

No having to learn XML format gibberish, either.