What is Caché Object Script's method for passing parameters to a base constructor? - intersystems-cache

What is Caché Object Script's method for passing parameters to a base constructor?
For example, in C# you would do the following:
public MyConstructor(string id) : base(id) { }
where id is the value you wish to pass to the base constructor.

Do ##super(id)
super must be in lower case.

Related

not able to use DISTINCT or group by keyword in codeigniter

This is my controller.php:
$courserecord = $this->front->get_data_wheree('tbl_course_offered.course_id',array('isactive'=>'1'));
This is my Model.php:
function get_data_wheree($table,$where)
{
return $this->db->group_by($table,$where)->result();
}
In my model.php I want to use DISTINCT OR GROUP_BY in the Query.
The group_by is not working properly.
Your model is not working because the result() method is a member of the class CI_DB_result but group_by() returns an instance of CI_DB_query_builder. In other words, you are trying to call a method that does not exist for the class being used.
The methods get() and get_where() are the only two Query Builder methods that return a CI_DB_result object. Put simply, you have to call get() or get_where() before you can use result() or any similar methods.
The other problem is that group_by() does not take a second argument of the type you provide. You're providing an array but it expects a boolean. You need to add another method to define a where condition.
Your model's method should look like the following.
I changed the name of the first argument to better describe its purpose.
function get_data_where($groupby, $where)
{
return $this->db
->group_by($groupby)
->where($where)
->get('tbl_course_offered')
->result();
}

Can you pass by reference in PeopleCode?

I'm new to PeopleCode and as I'm learning functions, I noticed that in PeopleCode, we'd normally pass value using %PATIENT_ID. A friend told me that you can also pass by reference in PeopleCode but how?
PeopleCode passes by reference for functions.
Function addOne(&num As integer)
&num = &num + 1
End-Function;
Local integer &val = 9;
addOne(&val);
MessageBox(0, "", 0, 0,String(&val));
Results in 10
If you are using App Classes it behaves differently
for methods:
Pass by value for simple types (string, int, number,etc)
Pass by reference for objects (rowsets, records, app classes)
Can pass by reference for simple types using the OUT keyword in the parameter list
method addOne(&num as integer out)
Functions which are defined in the same context as the executing code, e.g. page/component/record/field event PeopleCode, always consider parameters as refernces.
Within Application Classes, parameters of simple types on methods can be defined with the 'out' key word to state that they are a references. Methods also automatically pass parameters as references for complex types. Think: "If there is a lot of data, it is a reference"
This documentation will be very helpful for you.
https://docs.oracle.com/cd/E26239_01/pt851h3/eng/psbooks/tpcr/chapter.htm?File=tpcr/htm/tpcr07.htm
Passing Parameters with Object Data Types
Parameters with object data types are always passed by reference:
/* argument passed by reference */
method storeInfo(&f as File);
If you specify the out modifier for a method parameter with an object
data type, it becomes a reference parameter. This means that the
parameter variable is passed by reference instead of the object that
it is pointing at when passed.
For example, if you pass an object parameter with the out modifier:
method myMethod(&arg as MyObjectClass);
Local MyObjectClass &o1 = create MyObjectClass("A");
Local MyOtherObjectClass &o2 = create MyOtherObjectClass();
&o2.myMethod(&o1);
And inside myMethod this occurs:
Method myMethod
&arg = create MyObjectClass("B");
end-method;
Since the method argument is reassigned within the body of myMethod,
&o1 does not point at the new instance of MyObjectClass (initialized
with "B") after the method call completes. This is because &o1 still
references the original instance of MyObjectClass.
However, if &o1 had been passed with the out modifier, after the
method call completes, &o1 points at whatever the parameter was last
assigned to; in this case, the new instance of MyObjectClass. The
parameter, rather than the object, is passed by reference. Using
the Out Specification for a Parameter
In the following example, a class, AddStuff, has a single public
method, DoAdd. This adds two numbers together, then assigns them as
different numbers. In the signature of the method declaration, the
first parameter is not declared with an out statement, while the
second one is.
class AddStuff
​method DoAdd(&P1 as number, &P2 as number out);
​end-class;
method DoAdd
&X = &P1 + &P2;
&P1 = 1;
&P2 = 2;
end-method;
In the following PeopleCode example, an object named &Aref is
instantiated from the class AddStuff. Two parameters, &I and &J are
also defined.
local AddStuff &Aref = Create AddStuff();
local number &I = 10;
local number &J = 20;
The following code example is correct. &J is changed, because of the
outstatement in the method signature, and because the value is being
passed by reference. The value of &I is not updated.
&Aref.DoAdd(&I, &J); /* changes &J but not &I */
The following code example causes a design time error. The second
parameter must be passed by reference, not by value.
&Aref.DoAdd(10, 20); /* error - second argument not variable */

Dozer Mapping with custom argument

I would need to map classA fields to classB fields along with localization i.e ClassA field values needs to be converted to localized value before it mapped to classB field. Locale should be passed as an argument to mapper in order get the localized value. Is there any option to pass runtime argument to mapper along with Source and Target classes?
Thanks.
Yes, you can do this. Let's get this example from Dozer docs
BeanMappingBuilder builder = new BeanMappingBuilder() {
protected void configure() {
mapping(Bean.class, Bean.class,
TypeMappingOptions.oneWay(),
mapId("A"),
mapNull(true)
)
.exclude("excluded")
.fields("src", "dest",
copyByReference(),
collectionStrategy(true,
RelationshipType.NON_CUMULATIVE),
hintA(String.class),
hintB(Integer.class),
FieldsMappingOptions.oneWay(),
useMapId("A"),
customConverterId("id")
)
.fields("src", "dest",
customConverter("org.dozer.CustomConverter")
);
}
};
Here we can find an example of dynamically configuration definition. Take a look at this part
customConverter("org.dozer.CustomConverter")
Here you can define a custom converter using this method
FieldsMappingOption customConverter(final String type)
But it has another version
customConverter(final Class<? extends CustomConverter> type, final String parameter)
And that's your case. You can write smth like
customConverter(com.yourproject.TranslatorConverter.class, "en")
in your dynamic code base config to define a parameter for you converter. How to write an implementation of CustumConverter which apply a parameter - take a look here

How to add a call to a non-static class member function to the threadpool?

I'm looking for this stackoverflow: How to get Windows thread pool to call class member function? for C++/CLI:
I have a ref class with a member function (a copy of that function is static for testing purposes):
ref class CTest
{
public:
static void testFuncStatic( System::Object^ stateInfo )
{
// do work;
}
void testFunction( System::Object^ stateInfo )
{
// do work;
}
};
From main() I can easily add a call to the static function to the threadpool:
System::Threading::ThreadPool::QueueUserWorkItem (gcnew System::Threading::WaitCallback (&CTest::testFuncStatic));
But I don't want to call the static function (which is more or less an object-independent global function), I want to call the member function testFunction() for several instances of the class CTest.
How can I achieve that?
In C++/CLI, you need to explicitly specify the object you want the delegate to call the function on.
ThreadPool::QueueUserWorkItem(gcnew WaitCallback(this, &CTest::testFunction));
^^^^
You should not use thread pools in .NET. You should consider to use System::Threading::Tasks. This is an even more efficient way to use multiple "Tasks"...
Also be aware of the new "async" keyword in C#4.5. This helps a lot! So you should really consider to put the .NET part of your application into C#... and only use C++/CLI for InterOp scenarios.
Try this:
CTest ^ ctest = gcnew CTest;
ThreadPool::QueueUserWorkItem(gcnew WaitCallback(ctest, &CTest::testFunction));
^^^^^
WaitCallback(ctest provides memory context to allocated object of CTest
&CTest::testFunction)); provides memory shift to actual allocated function memory address of testFunction.
'Dynamic' functions are part of 'dynamic' class object.
This must be like that because of garbage collector.

Need help understanding Generics, How To Abstract Types Question

I could use some really good links that explain Generics and how to use them. But I also have a very specific question, relater to working on a current project.
Given this class constructor:
public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity>
where TDomainContext : DomainContext, new()
where TEntity : Entity, new()
public SecuredDomainViewModel(TDomainContext domainContext, ProtectedItem protectedItem)
: base(domainContext)
{
this.protectedItem = protectedItem;
}
And its creation this way:
DomainViewModel d;
d = new SecuredDomainViewModel<MyContext, MyEntityType>(this.context, selectedProtectedItem);
Assuming I have 20 different EntityTypes within MyContext, is there any easier way to call the constructor without a large switch statement?
Also, since d is DomainViewModel and I later need to access methods from SecuredDomainViewModel, it seems I need to do this:
if (((SecuredDomainViewModel<MyContext, MyEntityType>)d).CanEditEntity)
But again "MyEntityType" could actually be one of 20 diffent types. Is there anyway to write these types of statements where MyEntityType is returned from some sort of Reflection?
Additional Info for Clarification:
I will investigate ConstructorInfo, but I think I may have incorrectly described what I'm looking to do.
Assume I have the DomainViewModel, d in my original posting.
This may have been constructed via three possible ways:
d = new SecuredDomainViewModel<MyContext, Order>(this.context, selectedProtectedItem);
d = new SecuredDomainViewModel<MyContext, Invoice>(this.context, selectedProtectedItem);
d = new SecuredDomainViewModel<MyContext, Consumer>(this.context, selectedProtectedItem);
Later, I need to access methods on the SecuredDomainViewModel, which currently must be called this way:
ex: if (((SecuredDomainViewModel<MyContext, Order)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Invoice)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Consumer)d).CanEditEntity)
Assuming I have N+ entity types in this context, what I was hoping to be able to do is
something like this with one call:
ex: if (((SecuredDomainViewModel<MyContext, CurrentEntityType)d).CanEditEntity)
Where CurrentEntityType was some sort of function or other type of call that returned Order, Invoice or Consumer based on the current item entity type.
Is that possible?
You can create a non-generic interface that has the CanEditEntity property on it, make SecuredDomainViewModel inherit off that, then call the property through the interface...
Also, the new() constructor allows you to call a constructor on a generic type that has no arguments (so you can just write new TEntity()), but if you want to call a constructor that has parameters one handy trick I use is to pass it in as a delegate:
public void Method<T>(Func<string, bool, T> ctor) {
// ...
T newobj = ctor("foo", true);
// ...
}
//called later...
Method((s, b) => new MyClass(s, b));
I can't help on the links, and likely not on the type either.
Constructor
If you have the Type, you can get the constructor:
ConstructorInfo construtor = typeof(MyEntityType).GetConstructor(new object[]{TDomainContext, ProtectedItem});
Type
I'm not really sure what you're looking for, but I can only see something like
if (((SecuredDomainViewModel<MyContext, entityType>)d).CanEditEntity)
{
entityType=typeof(Orders)
}
being what you want.