08-23-2008, 05:55 AM
Could I get an example or two on user defined classes. I am tryitng to make heads and tails of the help file and not getting too far.
Thanks.
Thanks.
Classes
|
08-23-2008, 05:55 AM
Could I get an example or two on user defined classes. I am tryitng to make heads and tails of the help file and not getting too far.
Thanks.
08-23-2008, 12:30 PM
The Classes topic in QM help is for those that already know what is a class. Most programming languages have classes.
To understand user-defined classes, you have to be familiar with variables, user-defined types and user-defined functions. A class is an user-defined type that also has functions. Let's create a simple rectangle class. Class definition includes class name and member variables. Macro Put it in some macro and compile or run the macro. It lets QM know about the new class. To make it always available, put it in a function that runs at startup. For example, in init2 (create it if does not exist). Now create 3 member functions. To add member functions use menu File -> New -> New Member Function. The item name consists of class name, . and function name. Member function CRect.Init function double'width double'height Member function CRect.Area Member function CRect.Hypotenuse function'double Now the class is created and you can use it anywhere. Declare variables of CRect type and call functions using syntax variable.Function(arguments). Macro
08-23-2008, 12:56 PM
You can create new classes that inherit member variableas and member functions of existing classes. Let's create a class that inherits from CRect.
Macro Member function CColorRect.Init function double'width double'height color Member function CColorRect.GetColor Example: Macro
08-23-2008, 01:20 PM
By default, member variables and functions are public. They can be accessed like
Macro To protect member variables from accessing from outside of the class, in class definition add one or two hyphens before these members: Macro class CRect Now r.m_width=15 would generate error. You also can protect some member functions from callig from outside the class. You can do it in function's Properties dialog. To hide member variables and functions without protecting, let the variable/function name begin with __. Examples: __m_hidden, CRect.__Hidden. Or place the functions in a folder that has 'private functions' checked in Folder Properties dialog.
08-23-2008, 01:34 PM
Member functions always are called with a variable of that type. Example:
Macro If Func want to access the variable (r1, r2 or other), it can use this. It is a reference to the variable for which the function called. Example: Member function CRect.Func
08-23-2008, 01:54 PM
A class can optionally have constructor, destructor and operator=. To add them, use item names like in the examples.
Constructor Member function CRect Destructor Member function CRect. Operator = Member function CRect= function CRect&source Test: Macro
08-23-2008, 06:29 PM
Thanks for all the information. I've only been able to skim over it so far, but it looks like you did a very good job explaining everything I wanted to know. Thanks a bunch!
09-29-2009, 05:37 PM
Could you give an example of how to create a new member function for a str variable?
str s.MemberFunction("data") out s where the member function formats the data and assigns it to str s. Thanks, jimmy Vig
09-29-2009, 07:25 PM
Inside the function use this as variable name.
function $data this.from(data "more data")
09-30-2009, 07:04 AM
this...totally right on.
str s="800.32" s.SecondsDateTime(s) out s Member function str.SecondsDateTime Totally stoked about knowing how to do this. Can definitely speed up and clean up coding! I've never been able to get classes to work right! Now I see what I was doing wrong ![]() Thank you so much! Jimmy Vig
09-30-2009, 07:39 AM
function $data
this=TimeSpanToStr(TimeSpanFromStr(data) 2) |
« Next Oldest | Next Newest »
|