Thoughts from Dan Miser RSS 2.0
 Sunday, June 13, 2004
Even though this is a .NET post, the concept absolutely transfers to classic Delphi as well. The other day, I needed to solve a problem in C#. Given a pseudo-code architecture like this:

class Base {}

class Concrete : Base {}

class Normal
{
  public Concrete ConcreteProp { get; set; }
}
and usage like this:

Base prop = new Concrete();
Normal obj = new Normal();
obj.ConcreteProp = prop;
I would receive (rightfully) a compiler error. Now, understand a few points before firing off comments.
  • The architecture must remain as-is. The classes above are actually FCL classes which I obviously can't change.
  • The usage needs to remain similar to the one shown above. I want to be able to store a reference to any class that descends from Base and use it in Normal.ConcreteProp later on.
  • Obviously, I could just throw a typecast in here to make things work, but this is the end-result of the test case. My requirements also dictated usage of Activator.CreateInstance and Reflection. I can't add references and typecasts to things I don't know about, so this must be truly dynamic.
.NET (and Delphi) don't have any concept of dynamic typecasting. So in order to solve the problem, I ended up taking a different approach. By using an extra Activator.CreateInstance to create a new Concrete obejct, I can then use that new object to assign over with no problems. After creating the object, I then copy over all of the base properties to the newly created object. So far, this has worked pretty well. However, it is not without it's downsides. The main drawbacks I see to this approach are:
  • I am not using the same object reference I stored, which means more memory usage
  • I cannot easily assign all properties from the cached Concrete object to the newly created one. In order to do this fully, I'll have to use Reflection to walk through all of the properties and set them all (if possible). For now, I know which properties absolutely must be assigned, so I just assign those.

Edited to add: OK, so I butchered the problem description in my haste. Mea culpa. I think I just stumbled into one of my own pet peeves. :-) Here's the actual code that doesn't work in case you're interested:


System.Data.Common.DbDataAdapter adapter = sqlDataAdapter1; 
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(); 
cb.DataAdapter = adapter; 

 

Sunday, June 13, 2004 3:49:00 PM (Central Standard Time, UTC-06:00)  #    Comments [3] -
Delphi
Tracked by:
http://9nj-information.info/68236104/catholic-confession.html [Pingback]
http://9ne-information.info/05496753/index.html [Pingback]
http://9no-information.info/46063300/load-flow-electric-power-system.html [Pingback]
http://9nh-information.info/50187820/christian-artis.html [Pingback]
http://9nm-information.info/95021500/index.html [Pingback]
http://9nv-information.info/03193338/life-of-ball-joints.html [Pingback]
http://9nj-information.info/54060943/index.html [Pingback]
http://9nx-information.info/01103318/index.html [Pingback]
http://9ne-information.info/21840437/southwest-high-school-miami-fl.html [Pingback]
http://9nw-information.info/09803753/index.html [Pingback]
http://9oi-information.info/54352422/index.html [Pingback]
http://9qe-information.info/79343797/schema-meccanico-attuatore-pneumatico.html [Pingback]
http://9om-information.info/62460634/index.html [Pingback]
http://9og-information.info/90743557/index.html [Pingback]
http://9og-information.info/86908493/index.html [Pingback]
http://9on-information.info/04951268/lita-s-entrance-song.html [Pingback]
http://9ou-information.info/44500599/music-lyrics-juanita.html [Pingback]
http://9ot-information.info/37971458/index.html [Pingback]
http://9ou-information.info/45627819/index.html [Pingback]
http://9qi-information.info/64255095/index.html [Pingback]
http://9rp-information.info/69285253/index.html [Pingback]
http://9ro-information.info/97466939/index.html [Pingback]
http://9sn-information.info/02111459/index.html [Pingback]
http://9sg-information.info/58772067/index.html [Pingback]
http://9rm-information.info/78838743/index.html [Pingback]
http://9rf-information.info/37402607/tent-city-jail.html [Pingback]
http://9rn-information.info/88554439/blow-me-in-my-car.html [Pingback]
http://9rr-information.info/44557014/index.html [Pingback]
http://9uafj-le-informazioni.info/62822730/speciale-salone-parigi-2006.html [Pingback]
http://9uaes-le-informazioni.info/66815986/just-looking.html [Pingback]
http://9uafc-le-informazioni.info/05399531/marxiana-theory-of-value.html [Pingback]
http://9uael-le-informazioni.info/68171057/index.html [Pingback]
http://9uafk-le-informazioni.info/26971536/andrea-santoni.html [Pingback]
http://9uaes-le-informazioni.info/84989905/index.html [Pingback]
http://9uaeg-le-informazioni.info/10617234/epatopatia-cronica-autoimmune.html [Pingback]
http://9uafo-le-informazioni.info/40008421/index.html [Pingback]
http://9uaff-le-informazioni.info/18268630/dm10-codici.html [Pingback]
http://9uaeh-le-informazioni.info/50688654/mappa-linea-tram-roma.html [Pingback]
http://9uahs-le-informazioni.info/47925842/index.html [Pingback]
http://9uagb-le-informazioni.info/09334685/come-se-tu.html [Pingback]
http://9uags-le-informazioni.info/49951363/parole-di-vita-eterna.html [Pingback]
http://9uahs-le-informazioni.info/13668269/index.html [Pingback]
http://9uahs-le-informazioni.info/66448223/index.html [Pingback]
http://9uagk-le-informazioni.info/25033629/index.html [Pingback]
http://9uagf-le-informazioni.info/85116148/index.html [Pingback]
http://9uahs-le-informazioni.info/11059024/index.html [Pingback]
Monday, June 14, 2004 2:57:00 AM (Central Standard Time, UTC-06:00)
"I woul receive (rightfully) a compiler error"



eh - why, exactly?
Hallvard Vassbotn
Monday, June 14, 2004 7:43:00 AM (Central Standard Time, UTC-06:00)
Yeah, yeah. That's what I get for abstracting the problem out to that level. :-)



When you map the classes like this:

Base = DbDataAdapter

Concrete = SqlDataAdapter

Normal = SqlCommandBuilder



you end up with the behavior mentioned above. For example,

System.Data.Common.DbDataAdapter adapter = sqlDataAdapter1;

System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder();

cb.DataAdapter = adapter;





Next time I'll check my abstractions before posting them. :)
Friday, November 03, 2006 4:28:00 AM (Central Standard Time, UTC-06:00)
Can we do this type of casting?

a ca = new a();

b cb = (b) ca;



where a and b are classes and they have identical members and not inhereted
Harsha
Comments are closed.
Navigation
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Dan Miser
Sign In
Statistics
Total Posts: 310
This Year: 25
This Month: 0
This Week: 0
Comments: 605
All Content © 2008, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)