SubTypes and Virtual Methods

Posts   
 
    
JoeriSoete
User
Posts: 14
Joined: 26-Sep-2007
# Posted on: 10-Apr-2008 17:27:33   

Hi,

Having some trouble with subtypes and virtual methods ...

Why does this code use the overriden function on the subtype class:

            ArtikelCollection _Artikels = new ArtikelCollection();
            IPredicateExpression _Filter = new PredicateExpression();
            _Filter.Add(new FieldCompareValuePredicate(ArtikelFields.Id, ComparisonOperator.Equal,107));
            _Artikels.GetMulti(_Filter);
            _Artikels[0].getReferentieArtikels(1043);

And this code the virtual function on the base class:

            ArtikelEntity _Artikel = new ArtikelEntity(107);
            _Artikel.getReferentieArtikels(1043);

ArtikelEntity is the base class, Entity 107 is of SubType VerpakkingArtikelEntity getSubcategorieen is defined as Virtual on the base class ArtikelEntity, and as override on the derived VerpakkingArtikel entity.

Thanks,

Joeri

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2008 10:56:33   

ArtikelCollection _Artikels = new ArtikelCollection(); IPredicateExpression _Filter = new PredicateExpression(); _Filter.Add(new FieldCompareValuePredicate(ArtikelFields.Id, ComparisonOperator.Equal,107)); _Artikels.GetMulti(_Filter); _Artikels[0].getReferentieArtikels(1043);

In the above code, the fetch is polymorphic, so actually the entity fetched inside the ArtikelCollection is of type VerpakkingArtikelEntity. This explains why the subtype method is the one getting called.

JoeriSoete
User
Posts: 14
Joined: 26-Sep-2007
# Posted on: 11-Apr-2008 11:00:22   

How do i go about to code a polymorphic fetch without using collections ...

In short, how can i correct this code ...

            ArtikelEntity _Artikel = new ArtikelEntity(107);
            _Artikel.getReferentieArtikels(1043);

Without knowing, beforehand, the correct subtype of the entity to fetch?

Thanks,

Joeri

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2008 11:13:28   
ArtikelEntity _Artikel = ArtikelEntity.FetchPolymorphic(null, id, null);

LLBLGen Pro will then create an instance of the entity type represented by id and will return that. _Artikel can then be of a different type. This works because ArtikelEntity is a supertype of the subtypes which can be produced.

JoeriSoete
User
Posts: 14
Joined: 26-Sep-2007
# Posted on: 11-Apr-2008 11:20:19   

Thanks for your fast and helpfull answer