Execute a class function based on a property

Asked

Viewed 126 times

1

Please, I need to execute a function of a class among several classes that I only know is correct from the value of one of its properties.

I create the classes for example:

 public tabelaSerie = new clsTabela('Serie');
 public tabelaAlunos = new clsTabela('Alunos');
 public tabelaNotas = new clsTabela('Notas');

So I instate a class setting the property "tableName" with the table name in use in this class.

The class has the function "locate(key)". Now I need to use the function "locate" in a class that has the property "tableName" equal to "Students".

And there’s no way I can do it this way (hypothetical example):

clsTabela('Alunos').locate(chave);

That is, use the locate function in the class that has the tableName property equal to "Students" but I do not know which of these instantiated classes is.

I would have to go through the classes one by one checking which of them has this property set with this value "Students" and then perform the locate function of this class. Something like checking with a typeof (but not with typeof)

And I’ll also need to know how I find a list of objects to identify which of these instantiated classes.

Hypothetical Example:

matricula = 543768776;
objetos = >>Objetosqueexistem<< // Não sei de onde obter esta relação de objetos
foreach (var obj in objetos) {
    if ( obj.hasOwnProperty('tableName') ) {
        if ( obj.valueOfProperty('tableName') == 'Alunos' ) {
           obj.locate(matricula);
        }      
    }

Logically in practice it is not easy to know which class, in my use this class may vary, it may not be exactly "Students", I will need to do this execution within the base class, I am, after she is instantiated she will have to perform the locate in another class that depends on which she herself is. Many people always ask what use to be able to reason better in the answer so I explain that it is unpredictable even to know which class I will perform the function, I will have to locate itI would then apply the function with parameters because this execution will take place only within the class, it is not an execution in my code at the level of my business rules.

Thank you for your attention, hug!

  • 1

    Honestly this is a wrong solution, if you used what the language already has would be much simpler. Use a array good and associative. These classes are only embellishments that do not bring usefulness.

  • Maniero, by the time of your answer I see that you have not read my edition where I added from "Hypothetical Example:" until the end. I disagree with your point of view. I created classes that allow me today to create an ERP in half an hour, and like now, I’m looking for more knowledge to further enhance their functionality. Sometimes we think that the person is going the wrong way and beyond answering exactly what he asks we try to steer towards a better solution and I appreciate when it comes out better than we expect because it changes the course of everything for the better. But at first I really want this answer.obg.

  • Ah, okay, now I get it, is that I worked at a company that hundreds of qualified engineers took decades to create an ERP, so I’m not the right person to answer that. Good luck, you’ll need.

  • Calm friend, even so I would like you to continue following because they are the minimum things that solve the biggest problems, with the development of the question can be you to know the detail that will help me. Thank you, hug!

  • @Marcelovareladasilva, did you try to create an array, key/value to store instances of your classes? From the key you could get the instance, regardless of the type and run the locate method. For example teste: { [tableName: string]: {} } = {};, you would add teste['Serie'] = new clsTabela('Serie'); and would obtain with let obj = teste['Serie']; obj.locate(chave);

  • @Kelvynrisso , Yes, I had tested it so and I can access the values but when I modify them they are not modified in the source, as a twowaydatabinding, and in fact my main goal is not only to get a value but also to set a value. I also tried to get an input but it didn’t work. I am searching typescript to know how to pass this parameter but in twowaydatabinding condition. So when I change from record in the Master table to Masterdetail table watching that value reload your data. But the way is right there, thank you for your attention! hug!

  • @Kelvynrisso ... explaining better... in this way passing as array parameter when I change from record in the Master to Master table that was passed as static parameter, it does not follow the changes of the Master origin. The only way I managed was to pass the Master as DI (dependency injection). Or looking outside the class.

  • @Kelvynrisso ... if it was a visual component I would pass a [(tabelaMaster)]="tblMaster" and inside with @ Input(tblMaster) i would manipulate the Origin Master table but am not hitting and finding material on the net that explains how I pass a parameter to a class in the twowaydatabinding condition.

  • @Marcelovareladasilva, I don’t know if I got it right, but when you used the array approach, you kept the property statement that way: public tabelaSerie = new clsTabela('Serie'); and that’s the one that’s not changed, that’s it?

  • @Kelvynrisso public tblMaster - new clsTabelas('Master');

  • @Marcelovareladasilva, and you added in the array teste['Master'] = this.tblMaster or teste['Master'] = new clsTabelas('Master')?

  • @KelvynRisso&#xA;public tblMaster = new clsTabelas('Master');&#xA;public tblDetail = new clsTabelas('Detail',{'MasterSource': this.tblMaster});&#xA;&#xA;this.tblMaster.locate(4);&#xA;A tabela tblDetail automaticamente leria os dados da chave master '4'. But for me to observe this change within the new class created tblDetail the Mastersource parameter wants the tblMaster class object would have to be twowaydatabinding and not static as it looks.

  • @Marcelovareladasilva, I think I understand, typescript/javascript, by default would receive object type parameters by reference, so you change either side, both should be changed. So in this case it could be a scope problem, and your property is only valid in the scope of creation. Maybe you can change your instance to public tblDetail = new clsTabelas('Detail', this.tblMaster); to see if it has an impact.

  • @Kelvynrisso, yes, that’s how I spoke with dependency injection that works. It is that for a current and mainly future organization I would like to pass the object in the parameter array or pass the string with the name only and inside access the object.

  • @Kelvynrisso Currently I do so public tblDetail = new clsTabelas('Detail', this.tblMaster, this._Http);, I have to inject the master table and also httpclient because I can’t instantiate it inside the class, when it was only http I instantiated inside in about 6 lines, but now I can’t do it anymore.

Show 10 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.