BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

need urgent help for mysterious typecasting error...
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Christodoulos Pantazis
Guest





PostPosted: Tue Dec 16, 2003 4:33 pm    Post subject: need urgent help for mysterious typecasting error... Reply with quote



I would be really grateful if you someone could lend me a hand with the
following.

I have writte my own component, which it looks like this:

---
unit myControls;
interface

uses SySutils, Classes, Controls, ComCtrls, Contnrs, Dialogs, Hashes;

type myNode = class (TTreeNode)
private
node_id: String;
description: String;
value: Integer;
parent_id: String;
level: Integer;
memo: String;
public
procedure setNode_id(const Value: String);
procedure setDescription(const Value: String);
procedure setValue(const i: Integer);
procedure setLevel(const i: Integer);
procedure setParent_id(const Value: String);
procedure setMemo(const Value: String);
function getNode_id: String;
function getDescription: String;
function getValue: Integer;
function getParent_id: String;
function getLevel: Integer;
function getMemo: String;

end;

type myTreeView1 = class (TTreeView)
private

public
procedure addNode(node: myNode; node_id: String; desc: String; value:
Integer; base: Boolean; var parent: myNode); overload;
procedure addNode(node: myNode; node_id: String; desc: String; value:
Integer; parent_id: String); overload;
procedure removeNode(node: myNode);
function getTotalPercentScoreForNode(parent: myNode): Integer;
function findNode(pid: String): myNode;
function getAbsolutePath(pid: String): String;
procedure getAllChildNodes(node: myNode; var list: TStringList);
function calculateScore(node: myNode; table: TStringHash): Double;
function getScore(node: myNode; table: TStringHash): Double;
function CountNodes(node: myNode): Integer;
end;
---

in this unit, I create two classes: myTreeNode1 and myNode.

However when I try to do the following:

var
node: myNode;
begin
node := tree.Items.AddChild(nil, 'test') as myNode;
end;

it gives me a Typecasting Error. I suppose it cannot cast Items.AddChild (it
returns pointer to TTreeNode) to myNode.

I really need your input on that!!


Back to top
Christodoulos Pantazis
Guest





PostPosted: Tue Dec 16, 2003 5:23 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote



It seems that I corrected sth..

I tried the casting in this way and it works:

node := myNode(tree.Items.AddChild(nil, 'test'));

instead of

node := tree.Items.AddChild(nil, 'test') as myNode;

but can anyone tell me what's the difference between those two castings?


"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote

Quote:

I would be really grateful if you someone could lend me a hand with the
following.

I have writte my own component, which it looks like this:

---
unit myControls;
interface

uses SySutils, Classes, Controls, ComCtrls, Contnrs, Dialogs, Hashes;

type myNode = class (TTreeNode)
private
node_id: String;
description: String;
value: Integer;
parent_id: String;
level: Integer;
memo: String;
public
procedure setNode_id(const Value: String);
procedure setDescription(const Value: String);
procedure setValue(const i: Integer);
procedure setLevel(const i: Integer);
procedure setParent_id(const Value: String);
procedure setMemo(const Value: String);
function getNode_id: String;
function getDescription: String;
function getValue: Integer;
function getParent_id: String;
function getLevel: Integer;
function getMemo: String;

end;

type myTreeView1 = class (TTreeView)
private

public
procedure addNode(node: myNode; node_id: String; desc: String; value:
Integer; base: Boolean; var parent: myNode); overload;
procedure addNode(node: myNode; node_id: String; desc: String; value:
Integer; parent_id: String); overload;
procedure removeNode(node: myNode);
function getTotalPercentScoreForNode(parent: myNode): Integer;
function findNode(pid: String): myNode;
function getAbsolutePath(pid: String): String;
procedure getAllChildNodes(node: myNode; var list: TStringList);
function calculateScore(node: myNode; table: TStringHash): Double;
function getScore(node: myNode; table: TStringHash): Double;
function CountNodes(node: myNode): Integer;
end;
---

in this unit, I create two classes: myTreeNode1 and myNode.

However when I try to do the following:

var
node: myNode;
begin
node := tree.Items.AddChild(nil, 'test') as myNode;
end;

it gives me a Typecasting Error. I suppose it cannot cast Items.AddChild
(it
returns pointer to TTreeNode) to myNode.

I really need your input on that!!





Back to top
Bruce Roberts
Guest





PostPosted: Tue Dec 16, 2003 5:39 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote




"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote


Quote:
I tried the casting in this way and it works:

node := myNode(tree.Items.AddChild(nil, 'test'));

As will

node := myNode (1234);

When one uses this type of cast one is informing the compiler to ignore all
its checking, that the programmer does indeed know what they are doing, and
what they are doing is correct.

Quote:

instead of

node := tree.Items.AddChild(nil, 'test') as myNode;

but can anyone tell me what's the difference between those two castings?

AS actually checks the result of AddChild to see if it is in fact a myNode.
(BTW it would be much clearer if you prefaced your type identifiers in the
conventional manner, i.e. tMyNode.) Since an exception is being raised, I
guess that tree.Items.AddChild is not returning a myNode.

I would never use the first type of cast if the second form (using AS or IS)
failed. Doing so is a sure recipe for bugs.



Back to top
Christodoulos Pantazis
Guest





PostPosted: Tue Dec 16, 2003 6:17 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

you are correct, because doing this

node := myNode(tree.Items.AddChild(nil, 'test'));

and then trying to

node.setNode_id('id'); //setNode_id is a function of myNode

gives me an error as if I never created 'node' object... what is wrong?

Do you think that there something wrong with the installation of my
component in Delphi7?
there is sth else that sounds strange. When I try to use F7 (trace into...
debugging function) it never
tries to jump into the source of my component, it throws the exception
immediately.



"Bruce Roberts" <ber (AT) bounceitattcanada (DOT) xnet> wrote

Quote:


"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote in message
news:1071594962.185561 (AT) athnrd02 (DOT) forthnet.gr...

I tried the casting in this way and it works:

node := myNode(tree.Items.AddChild(nil, 'test'));

As will

node := myNode (1234);

When one uses this type of cast one is informing the compiler to ignore
all
its checking, that the programmer does indeed know what they are doing,
and
what they are doing is correct.


instead of

node := tree.Items.AddChild(nil, 'test') as myNode;

but can anyone tell me what's the difference between those two castings?

AS actually checks the result of AddChild to see if it is in fact a
myNode.
(BTW it would be much clearer if you prefaced your type identifiers in the
conventional manner, i.e. tMyNode.) Since an exception is being raised, I
guess that tree.Items.AddChild is not returning a myNode.

I would never use the first type of cast if the second form (using AS or
IS)
failed. Doing so is a sure recipe for bugs.





Back to top
Rob Kennedy
Guest





PostPosted: Tue Dec 16, 2003 6:24 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

Christodoulos Pantazis wrote:
Quote:
It seems that I corrected sth..

You didn't correct anything. You only disguised the problem by telling
the compiler not to question your actions.

Quote:
I tried the casting in this way and it works:

node := myNode(tree.Items.AddChild(nil, 'test'));

instead of

node := tree.Items.AddChild(nil, 'test') as myNode;

but can anyone tell me what's the difference between those two castings?

If the second one doesn't work, then the first one isn't working,
either. In the first one, you're explicitly telling the compiler, "No
matter what you think AddChild returns, I'm absolutely certain that it
is really a MyNode object." The compiler trusts you.

In the second one, using the AS operator, you're telling the compiler,
"I want to use the result of AddChild as a MyNode instead of a
TTreeNode, so please confirm that the object really is a MyNode."

Since the AS cast is failing, it's an indication that when you use the
other casting, you are *lying* to the compiler. The object's type is
*not* MyNode. Since MyNode.InstanceSize <> TTreeNode.InstanceSize, this
will lead to major problems as soon as you start trying to use any of
the new fields you added to MyNode. The tree only allocated enough space
to hold a TTreeNode, not a MyNode.

If you want a different class of tree node, then you must override the
tree's CreateNode function. The base implementation creates and returns
a new TTreeNode object. You need to create and return a MyNode object
instead. Do not call inherited.

--
Rob

Back to top
Christodoulos Pantazis
Guest





PostPosted: Tue Dec 16, 2003 9:08 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

I have the feeling however, that there must be a problem with the
registration of the component
although it lets me use "uses" for it, it doesn't "allocate" memory for
myNode (which is also defined in the component) and myTreeView1 classes...


"Rob Kennedy" <.> wrote

Quote:

Christodoulos Pantazis wrote:
It seems that I corrected sth..

You didn't correct anything. You only disguised the problem by telling
the compiler not to question your actions.

I tried the casting in this way and it works:

node := myNode(tree.Items.AddChild(nil, 'test'));

instead of

node := tree.Items.AddChild(nil, 'test') as myNode;

but can anyone tell me what's the difference between those two castings?

If the second one doesn't work, then the first one isn't working,
either. In the first one, you're explicitly telling the compiler, "No
matter what you think AddChild returns, I'm absolutely certain that it
is really a MyNode object." The compiler trusts you.

In the second one, using the AS operator, you're telling the compiler,
"I want to use the result of AddChild as a MyNode instead of a
TTreeNode, so please confirm that the object really is a MyNode."

Since the AS cast is failing, it's an indication that when you use the
other casting, you are *lying* to the compiler. The object's type is
*not* MyNode. Since MyNode.InstanceSize <> TTreeNode.InstanceSize, this
will lead to major problems as soon as you start trying to use any of
the new fields you added to MyNode. The tree only allocated enough space
to hold a TTreeNode, not a MyNode.

If you want a different class of tree node, then you must override the
tree's CreateNode function. The base implementation creates and returns
a new TTreeNode object. You need to create and return a MyNode object
instead. Do not call inherited.

--
Rob



Back to top
Rob Kennedy
Guest





PostPosted: Tue Dec 16, 2003 9:25 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

Christodoulos Pantazis wrote:
Quote:
I have the feeling however, that there must be a problem with the
registration of the component
although it lets me use "uses" for it, it doesn't "allocate" memory for
myNode (which is also defined in the component) and myTreeView1 classes...

Your feeling is wrong. Override TTreeView.CreateNode.

The reason it doesn't allocate memory for MyNode is because you haven't
told it to create instances of MyNode. The CreateNode method is where
you do that.

--
Rob

Back to top
Bruce Roberts
Guest





PostPosted: Tue Dec 16, 2003 11:05 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote


"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote


Quote:
node := myNode(tree.Items.AddChild(nil, 'test'));

and then trying to

node.setNode_id('id'); //setNode_id is a function of myNode

gives me an error as if I never created 'node' object... what is wrong?

tTreeNode does not, to the best of my knowledge, have a SetNode_Id method.

Quote:
Do you think that there something wrong with the installation of my
component in Delphi7?

No.

Quote:
there is sth else that sounds strange. When I try to use F7 (trace into...
debugging function) it never
tries to jump into the source of my component, it throws the exception
immediately.

Because as I stated before, AddChild returns a node of type tTreeNode NOT
myNode. See Rob Kennedy's response(s) for what you should be doing to
correct the problem.

Why is it that new users tend to blame the tool before questioning their own
knowledge of its use?



Back to top
David Reeve
Guest





PostPosted: Wed Dec 17, 2003 12:11 am    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote

Quote:
I have the feeling however, that there must be a problem with the
registration of the component
although it lets me use "uses" for it, it doesn't "allocate" memory for
myNode (which is also defined in the component) and myTreeView1
classes...



What's component registration got to do with it? Rob's reply is 100%
correct. The information contained in a class is analogous to a map. If you
tell the compiler to use the wrong map you can expect strange things to
happen. You could insist on using a London street map in New York, but it's
probably just not worth the effort. :-)

Dave



Back to top
Christodoulos Pantazis
Guest





PostPosted: Wed Dec 17, 2003 7:23 am    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote


"Bruce Roberts" <ber (AT) bounceitattcanada (DOT) xnet> wrote

Quote:


"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote in message
news:1071598194.63386 (AT) athnrd02 (DOT) forthnet.gr...

node := myNode(tree.Items.AddChild(nil, 'test'));

and then trying to

node.setNode_id('id'); //setNode_id is a function of myNode

gives me an error as if I never created 'node' object... what is wrong?

tTreeNode does not, to the best of my knowledge, have a SetNode_Id method.


yes, but myNode (which extends tTreeNode) owns a setNode_id method (which I
created).
although tree.Items.AddChild() returns a pointer to tTreeNode, I think the
casting to myNode should
be safe.

Quote:
Why is it that new users tend to blame the tool before questioning their
own
knowledge of its use?

I am not blaming the tool. The problem is that a year ago I had created this
program which it was working perfectly. This year
I decided to make some modifications and I installed the Tool again, I
installed my component, loaded the project file and voila! The same code
that it was working last year, now it doesn't want to cooperate. That is why
I keep on asking if there is anything wrong with my installation of the
component.



Back to top
Christodoulos Pantazis
Guest





PostPosted: Wed Dec 17, 2003 7:26 am    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote


"Rob Kennedy" <.> wrote

Quote:

Christodoulos Pantazis wrote:
I have the feeling however, that there must be a problem with the
registration of the component
although it lets me use "uses" for it, it doesn't "allocate" memory for
myNode (which is also defined in the component) and myTreeView1
classes...

Your feeling is wrong. Override TTreeView.CreateNode.

The reason it doesn't allocate memory for MyNode is because you haven't
told it to create instances of MyNode. The CreateNode method is where
you do that.

thanks I'll try to do it and then reply with the results...



Back to top
Christodoulos Pantazis
Guest





PostPosted: Wed Dec 17, 2003 8:03 am    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

I just looked into Marco Cantu's "Mastering Delphi 6" book.

He has written the same example as I did.
TForm.TreeView1CreateNodeClass() which sets the NodeClass to TMyNode
(instead of overiding the "CreateNode" class of TreeView - I think) is also
there.

I copy from his book

var
MyNode: TMyNode;
begin
CurrentNodeClass := TMyNode; //NodeClass is set to CurrentNodeClass
which is of TMyNode type
MyNode := TreeView1.Items.AddChild(nil, 'test') as TMyNode;
MyNode.ExtraCode := nValue;
end;

//...

procedure TForm1.TreeView1CreateNodeClass(Sender TCustomTreeView; var
NodeClass: TTreeNodeClass)
begin
NodeClass = CurrentNodeClass;
end;

so although AddChild returns a pointer to TTreeNode the casting is safe to
TMyNode as long as the CurrentNodeClass is set
to TMyNode...

....and still mine doesn't work! :-(


"Christodoulos Pantazis" <xpan (AT) arg (DOT) forthnet.gr> wrote

Quote:


"Rob Kennedy" <.> wrote


Christodoulos Pantazis wrote:
I have the feeling however, that there must be a problem with the
registration of the component
although it lets me use "uses" for it, it doesn't "allocate" memory
for
myNode (which is also defined in the component) and myTreeView1
classes...

Your feeling is wrong. Override TTreeView.CreateNode.

The reason it doesn't allocate memory for MyNode is because you haven't
told it to create instances of MyNode. The CreateNode method is where
you do that.

thanks I'll try to do it and then reply with the results...





Back to top
Nicolai Hansen
Guest





PostPosted: Wed Dec 17, 2003 8:22 am    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

Quote:
var
node: myNode;
begin
node := tree.Items.AddChild(nil, 'test') as myNode;
end;

it gives me a Typecasting Error. I suppose it cannot cast Items.AddChild (it
returns pointer to TTreeNode) to myNode.

I really need your input on that!!

tree.items.AddChild adds and returns a TTreeNode, not a myNode.
If you want it to return a myNode, you should create your own items
class as well, and override tree.items to use this class.

Back to top
Maarten Wiltink
Guest





PostPosted: Wed Dec 17, 2003 11:57 am    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote

"Nicolai Hansen" <nic (AT) aub (DOT) dk> wrote


Quote:
var
node: myNode;
begin
node := tree.Items.AddChild(nil, 'test') as myNode;
end;

it gives me a Typecasting Error. I suppose it cannot cast
Items.AddChild (it returns pointer to TTreeNode) to myNode.

I really need your input on that!!

So that you can then tell everybody that _they_ don't know their
stuff, are resistent to learning, and that you will proceed to
ignore their advice?


Quote:
tree.items.AddChild adds and returns a TTreeNode, not a myNode.
If you want it to return a myNode, you should create your own items
class as well, and override tree.items to use this class.

A (T)MyNode IsA TTreeNode. You can make TTreeView.Items create
your own type of nodes by overriding the owning treeview's
CreateNode. The help says so. Rob also already said so.

What I really expected was a property ItemClass or NodeClass in
TTreeNodes, but it's not a TCollection. It derives directly from
TPersistent.

Groetjes,
Maarten Wiltink



Back to top
Christodoulos Pantazis
Guest





PostPosted: Wed Dec 17, 2003 2:22 pm    Post subject: Re: need urgent help for mysterious typecasting error... Reply with quote


Quote:
I really need your input on that!!

So that you can then tell everybody that _they_ don't know their
stuff, are resistent to learning, and that you will proceed to
ignore their advice?

didn't undestand what you mean by saying that. I just need your help,
nothing else. Of course I will not
ignore your advice, that is why I asked.

Quote:
What I really expected was a property ItemClass or NodeClass in
TTreeNodes, but it's not a TCollection. It derives directly from
TPersistent.

I have added:

procedure TMainForm.MyCustomTreeCreateNodeClass(Sender: TCustomTreeView; var
NodeClass: TTreeNodeClass);
begin
NodeClass := CurrentNodeClass; //where CurrentNodeClass is my custom
MyNode class
end;


which forces somehow (not sure tho) my Custom Tree type's AddChild to return
a Custom Node (MyNode) instead of a TTreeNode.

PS: I am sorry for not using T (for type) in my classes' names but I am
actually a Java Programmer.




Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.