Ecere SDK/eC Forums • View topic - How to access variables ?




Ecere SDK/eC Forums

Ecere Developers Community

How to access variables ?

General help with the eC language.

How to access variables ?

Postby nicktick » Tue Apr 16, 2013 10:00 pm

Code: Select all
 
class test : Window
{ 
   Point pos;
   Label label_1{
           void TestFunction()
            {
             this.position = !!!; // how to access the Point pos here ?
           }
   }   
}
 

How to access variable Point pos at '!!!' ?
nicktick
 
Posts: 17
Joined: Tue Nov 13, 2012 8:34 pm

Re: How to access variables ?

Postby jerome » Tue Apr 16, 2013 10:07 pm

Hi nicktick,

I'm not sure what you are trying to do here.

First you are missing a semicolon after: Label label_1 { ... } ;

Then, inside those brackets for the label you are setting the values for the Label members of label_1.
You cannot use 'this.' here , you are not inside a method.

Also, in eC you should avoid using 'this.' anywhere, unless you must resolve an ambiguity (which you are better off avoiding in the first place).

Note: you can use code=eC for the tags on the forums to get syntax highlighting.

Code: Select all
class test : Window
{ 
   Point pos;
   Label label_1{ position = pos };
}

This should work, but pos can only be set to the initial value of pos from the test default properties. So you might as well set that position directly onto the label_1, rather than keeping a member variable for it?

Regards,

-Jerome
jerome
Site Admin
 
Posts: 621
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to access variables ?

Postby jerome » Tue Apr 16, 2013 10:13 pm

Hi nicktick, did you just edit that post, or did I miss the TestFunction()?

You cannot add new methods to instances, you can only override virtual methods.

If the method is a regular virtual method

(e.g. virtual void OnMyVirtualMethod() -- This is for derivative controls to modify the behavior of 'this' control )

and the control has set its master to 'this' (The 'test' class).

Then you can do inside TestFunction:
Code: Select all
test testObject = (test)master;
position = testObject.pos;

If the method is an event (e.g. virtual void Window::NotifySomethingHappened() -- This is for masters (owners) of the control to handle something that happened to the control ('this' inside the overriden method will be the control's master) ) then the 'this' is already your test object.

Usually a parameter is included specifying the control generating the event, e.g.:
Code: Select all
void TestFunction(Label lbl)

and you could do:
Code: Select all
lbl.position = pos;

or you could address label_1 directly:
Code: Select all
label_1.position = pos;


-Jerome
jerome
Site Admin
 
Posts: 621
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to access variables ?

Postby nicktick » Tue Apr 16, 2013 10:33 pm

Code: Select all

class TestWinow : Window
{
   background = activeBorder;
   opacity = 0.5f;
   clientSize = { 576, 392 };
   position = { 112, 72 };
   alphaBlend = true;   

   Point deltaPos{0,0};

   Label label1
   {     
      this, caption = "label1", background = blue, 1, size = { 156, 85 }, position = { 160, 144 };

      void OnRedraw(Surface surface)
      {
         this.position += deltaPos; ///error: unresolved identifier deltaPos; expected ecere::sys::Point 
         Label::OnRedraw(surface);
      }
   }

   bool OnMouseMove(int x, int y, Modifiers mods)
   {
      deltaPos.x = x;
      deltaPos.y = y;
      return true;
   };
}

TestWinow mainForm {};


this source code can pass the compiler, there is no semi-colon after Label label1 {}
nicktick
 
Posts: 17
Joined: Tue Nov 13, 2012 8:34 pm

Re: How to access variables ?

Postby jerome » Tue Apr 16, 2013 10:39 pm

Hi nicktick,

1. Never change anything in OnRedraw. Only render the current state. Change things elsewhere.

Here you are overriding OnRedraw, which is a virtual method of the Label.
'this' represents the label itself. (Typically, virtual methods are not meant to be overridden at the instance level. Events are what is meant to be overridden at the instance levels, and inside them 'this' will be the master, i.e. your TestWindow)

Also, you have a typo , you wrote TestWindow 'TestWinow' without the d.

And x,y in OnMouseMove is not a delta, it is the coordinates of the cursor in the client space of your window.

You also need to invoke Update() for OnRedraw() to get called (and before calling Update(), that is normally where you would change something).

-Jerome
jerome
Site Admin
 
Posts: 621
Joined: Sat Jan 16, 2010 11:16 pm

Re: How to access variables ?

Postby nicktick » Tue Apr 16, 2013 11:00 pm

jerome wrote:Hi nicktick, did you just edit that post, or did I miss the TestFunction()?

You cannot add new methods to instances, you can only override virtual methods.

If the method is a regular virtual method

(e.g. virtual void OnMyVirtualMethod() -- This is for derivative controls to modify the behavior of 'this' control )

and the control has set its master to 'this' (The 'test' class).

I clicked the OnRedraw() method of label1 from IDE Methods window, then the IDE jumped to the definition of label1. so I didn't recognized that I couldn't change OnRedraw() there, but the IDE didn't give me any message on preventing me modifying itself.

how can I tell the label1 to update its position from a method of class TestWindow(and it will let the label1 aware of its position changed) ? I think that if I could add a custom event(event_a) to class Label, then I could fire event_a in method of class TestWindow, is it a workable way ? or what are the best way to reach the goal? since I will use some labels, so how can I custom the class Label to got the above goal ?
nicktick
 
Posts: 17
Joined: Tue Nov 13, 2012 8:34 pm

Re: How to access variables ?

Postby jerome » Wed Apr 17, 2013 12:15 am

Hi nicktick,

You can override OnRedraw at the instance level (because it is a virtual method).

However, that is not the typical usage. Typically for controls, the events are overridden at the instance level, while a class is derived to further customize the behavior of the control and override non-events virtual methods (On*).

To change the position of a label, just say label.position = { new position }.

A Label shouldn't care what its position is, it gets rendered wherever it is positioned.

'OnPosition' is the virtual method that is invoked when a Window is repositioned.

But as stated above, instances of controls should not need to override non-events virtual methods.

I don't really know what you are trying to accomplish here, so it's hard to suggest the best approach.
jerome
Site Admin
 
Posts: 621
Joined: Sat Jan 16, 2010 11:16 pm


Return to eC Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron