Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
10
Erster Beitrag:
vor 3 Jahren, 1 Monat
Letzter Beitrag:
vor 3 Jahren, 1 Monat
Beteiligte Autoren:
DerekT, David Egan, Alexandre Leclerc, Arie, Allard, Bart VDE

[WD] Dynamic Class Declaration

Startbeitrag von David Egan am 19.01.2015 07: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

Antworten:

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

von Allard - am 19.01.2015 09:33
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".

von Arie - am 19.01.2015 09:53
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

von Bart VDE - am 19.01.2015 10:28
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

von DerekT - am 19.01.2015 11:58
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

von Alexandre Leclerc - am 19.01.2015 13:27
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


von DerekT - am 19.01.2015 15:22
Thanks everyone for the response.

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

Cheers

David

von David Egan - am 19.01.2015 20:41
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

von Alexandre Leclerc - am 20.01.2015 14:32
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:

von DerekT - am 20.01.2015 15:11
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.