User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 373,930 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,272 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser:

delphi object casting

Join Date: Dec 2007
Location: Zagreb, Croatia
Posts: 4
Reputation: Smola is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Smola's Avatar
Smola Smola is offline Offline
Newbie Poster

Re: delphi object casting

  #5  
Dec 26th, 2007
Originally Posted by adrive View Post
i'm not sure if i'm getting this right, but i still remember java allowing me to cast an object to another type as long as their ancestors are the same?


Actually you can _cast_ any object to any class, but you cannot _assign_ an ancestor-object to a variable of its descendant class.

Therefore you cannot assign TStrings to the TStringList, only vice-versa.

Originally Posted by adrive View Post
I can't seem to do this in delphi, the compiler doesn't seem to understand the methods and attributes of each object even after I've casted them. What's wrong?

I cannot see a single casting in your code. Casting looks like something this:

myQuery := TQuery( myOraQuery );


Originally Posted by adrive View Post
I'm trying to figure out how to stick two database query objects under the same name as illustrated :

I think TOraQuery and TADOConnection don't have the same ancestor that would have properties like ExecSQL and SQL. So you must use the approach suggested by ExplainThat.


Originally Posted by adrive View Post
//casting qry as a TOraQuery object :
qry := TOraQuery.Create(nil);
qry := commonObj.GetConnection('DATABASE_A');

This is not casting. This is merely creating an instance of the TOraQuery and than losing that reference immedietely afterwards (which is a memory leak).

Originally Posted by adrive View Post
but what i really wanted to do was to be able to use one variable to represent different types of database connection, one was TADOConnection, another TOraQuery, both sharing same qry variable.

this is because they both have some similiar method names and thus i could probably reduce my code lines by eliminating the need to do alot of conditional statements and switch between different variable types.


Unfortunately, those similar method names cannot be exploited, you'll have to duplicate your code, or use an alternative connection component.

Hope I've shed some light.
Doom on, soldier.
Reply With Quote  
All times are GMT -4. The time now is 6:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC