Syntax for Instantiating Objects

General help with the eC language.
Post Reply
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Syntax for Instantiating Objects

Post by jerome »

sanyaade asked:
I have some questions on object creations:

Is this possible:-

class Myclass : Window { text ="I am a Window"}

Myclass newMyclass; // object creation/derivate
newMyclass.text="I am a new Myclass"; ---> this gives error

ControlsSample2* controlSample5; //{text="I am the twins";};
controlSample5 -> text("Hey bobby"); ---> this also give error

Lastly how do I enable code completion in Ecere SDK?
Here is a sample file I put together to attempt to clearly explain all about the instantiation syntax. Please review (in light of these examples and explanations) the first chapter of the second section of the Ecere Tao of Programming (Section 2 - Object Oriented Programming with eC - Classes, methods and instances), specifically page numbered 65-67.

About the IDE's AutoCompletion feature, it is always on.
If you have syntax error in your code however, although we try our best to make the IDE's parser understand the code and make some sense of it, it may get lost and not display anything.
We will be working on improving the reliability (and performance as well) of the AutoComplete in the upcoming release. In normal circumstances however it works.
The hotkeys for the AutoComplete are:
- Ctrl-Space to complete an identifier or to bring up potential identifiers (members, enumeration values, local and global variables, functions, etc.)
- Ctrl-Shift-Space to popup a function/method's parameters list or an instantiation's current default member at the cursor's location

Code: Select all

import "ecere"
class MyClass : Window
   // These are called 'class member default values'. Note that they require a semicolon at the end
   text = "I am a Window";
   hasClose = true;
   size = { 320, 200 };
// When you have a class data type ('MyClass' here) + an identifier ('myClassGlobalHandleOnly' here),
// but without the curly brackets '{' and '}', it is NOT an instantiation, but merely a handle which
// you can assign to an existing MyClass instance. Only global and member instances (instances inside
// other classes, which in turn get initialized) are reliably initialized to a 'null' (0) value.
// These are akin to a C/C++ pointer, but in eC we just think of it as a 'handle'.
MyClass myClassGlobalHandleOnly;
// This one here gets instantiated!
MyClass myInstantiatedObject { text = "Window 1", position = { 200, 200 } };
class MyApp : GuiApplication
   // The class derriving from the Application or GuiApplication class is the program entry point and the application class.
   // If none is defined and "ecere" is imported, the GuiApplication will be the default application class.
   // Therefore defining one is not required, defining Window instances (e.g. the default New Project/Create Form's 'Form1 form1 { };' instance)
   // is enough to get a GuiApplication going.
   // Here for the sake of explaining how to use instances within a function/method, we'll create some instances
   // in our Application class constructor. Note that because we do it here, before the GUI system is initialized,
   // the windows will all be auto-created. If we were to dynamically create window instances later on after the
   // parent window (the desktop) has already been created, we would need to additionally call Create() on each of
   // those new Window objects for them to appear on the screen.
      // Note that 'local' variables (variables inside compound blocks within functions) do NOT 
      // get initialized to 0, and contain whatever was on the stack, just like any other C/eC variable.
      MyClass myClassHandleOnly;    // C++ counterpart: MyClass * myClassHandleOnly; 
      // Here we assign a new instance to our handle. You could also write it: myClassHandleOnly = { };
      myClassHandleOnly = MyClass { };  // C++ counterpart:  myClassHandleOnly = new MyClass();
      // You can do this here. I would need to see your full source code to see what error you were getting.
      // Note that in eC you very rarely require the '->' operator, because class members can be accessed
      // from handles using the '.' operator. The use of '->' is limited to interfacing with C code,
      // or very specific cases involving struct data types.
      myClassHandleOnly.text = "Window 2";   
      // You could have done both assignment and setting the property at once with:
      // myClassHandleOnly = { text = "Window 2" };
      // Or do it all one one declaration line with a local instantiation:
      // MyClass myLocalClassInstance { text = "Window 2" };
      // Note that a named instantiation (e.g. myLocalClassInstance), a handle declaration (e.g. myClassHandleOnly), and a regular 
      // C variable declaration (e.g. int a;) are all 'declarations'
      // Anonymous instantiations (e.g.  MyClass { text = "Window 2" };) and assignments (e.g. myClassHandleOnly = MyClass { }; ) are all 'statements'
      // eC enforces (currently as an error, to be changed to a warning) all declarations to be at the top of a compound block ( Within curly brackets: { ... } )
      // This means that any declaration following a statement will cause a syntax error ( or a bunch of them! ), so beware!
      // A note about struct vs class: In eC there is a fundamental difference between struct and class.
      // Although eC was designed independently and without prior exposure from C#, the difference
      // between struct and class in both languages is very similar.
      // - struct instances are always allocated 'in place' (i.e. global storage, on the stack or in the containing object)
      // - class instances are always allocated on the heap
(4.09 KiB) Downloaded 2548 times
Post Reply