PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → [WD] Dynamic Class Declaration
[WD] Dynamic Class Declaration
Iniciado por guest, 19,ene. 2015 08:19 - 9 respuestas
Publicado el 19,enero 2015 - 08:19
Hi
For a generic data import routine I am writing I need to be able to declare a class which corresponds to the data file I am using. I have done something like this previously as follows MyClass is object dynamic SWITCH ls_filename CASE "CusMast": MyClass is NEW CusMastClass CASE "STKMast": MyClass is NEW STKMastClass etc etc I have quite a lot of data files which could potentially import data so what I would like to do is declare something like MyClass is NEW ClassName where ClassName is a string field from a data table, which holds "CusMastClass", "STKMastClass" etc.

Declared as above doesn't work & neither does indirection. Is anything like this possible or do I have to stick to a huge SWITCH statement?

Thanks

David
Publicado el 19,enero 2015 - 10:33
Hi ,

First I think you should not use the" : "

You could try something like this:

import is Class
END

CusMast is Class
inherits from import
END

SWITCH sfilename

Case CusMast

Case
End


But Iam not an experianced OOp user.

regards

Allard
Publicado el 19,enero 2015 - 10:53
David,

maybe the use of Compile() would work.
Build the code to compile as a string (using your data table, which holds "CusMastClass", "STKMastClass" etc.)
And then Compile and Execute this piece of code.
Please let us know the result, because that would be an interesting "feature".
Publicado el 19,enero 2015 - 11:28
I think something like this should work :

ClassName is string = "ClassPart"

myclass is object dynamic

myclass = new ClassName

myclass:PartNumber ="123"
myclass:PartDescription="This part description"
myclass:PartSellPrice=125
myclass:Add()

Regards,
Bart
Publicado el 19,enero 2015 - 12:58
David

I use the following.... simplified but should give you an idea

PROCEDURE lpImport(pTrgtClass is string) //Name of the target class TargetClass is string = pTrgtClass //Name of the class to use LocalClass is object dynamic //Declaration of local class LocalClass = new TargetClass //Instantiation of local class //Run a query to fetch data FileToArray(LocalClass:arr_Account,"DataSource") //Populate array with query result //OR// //Import of text or xls data ArrayAddLine(LocalClass:arr_Account,1,2) //Populate array with imported row content //Process the result FOR EACH DataRow OF LocalClass:arr_Account //Process the array content END
This assumes that you have an array declared in each of the target classes


Just noticed this is the same as Barts post - I concur
Publicado el 19,enero 2015 - 14:27
Hi David and Arie,

We use Compile() since years to do just that. In fact, now with the new ExecuteCode() syntax, this is easier for you.

o is a %1
RETURN o

We simply "pass" the class name (%) in parameter and the code returns an instance of the desired object. You can even use the ..Class property of an object later on to determine the class type.

Best regards,
Alexandre Leclerc
Publicado el 19,enero 2015 - 16:22
Alexandre

Just looking for clarification - experience tells me that there may always be a better way of doing things.

How is using ExecuteCode() better/easier/more efficient than the 3 lines of code I currently use ? - (Even my first line is optional)

PROCEDURE lpImport(pTrgtClass is string) //Name of the target class TargetClass is string = pTrgtClass //Name of the class to use LocalClass is object dynamic //Declaration of local class LocalClass = new TargetClass //Instantiation of local class
Publicado el 19,enero 2015 - 21:41
Thanks everyone for the response.

I've gone with Bart's/DerekT's suggestion. Works like a charm.

Cheers

David
Publicado el 20,enero 2015 - 15:32
Hi DerekT,

The only clarification is that I did not know it was possible to do it that way. We learn new stuff everyday. (In the case of using Compile(), ExecuteCode() was simpler.)

By the way, you can do it in one single line of code:

PROCEDURE lpImport(pTrgtClass is string) LocalClass is object dynamic = new pTrgtClass

Best regards,
Alexandre Leclerc
Publicado el 20,enero 2015 - 16:11
Alexandre

Thanks for the update.
You are correct - something new every day
This is one of the best NG's I have ever participated in - So much good info shared freely.:spos: