Board index » cppbuilder » Casting from AnsiString to TMenuItem

Casting from AnsiString to TMenuItem


2004-07-16 09:47:52 PM
cppbuilder66
Hi all, how do I cast an AnsiString text to TMenuItem object?
I've tried :
AnsiString A = "AddUser1";
dynamic_cast<TMenuItem *>(A)->Visible = false;
but does not succeed.
If casting by dynamic_cast is not possible, do we have any other
ways?
Thanks.
 
 

Re:Casting from AnsiString to TMenuItem

agus wrote:
Quote
.. how do I cast an AnsiString text to TMenuItem object?
That is an impossible operation!
What do you want ? That the contents of an AnsiString
equals TMeuItem::Caption ?
Hans.
 

Re:Casting from AnsiString to TMenuItem

"agus" < XXXX@XXXXX.COM >wrote in message
Quote
Hi all, how do I cast an AnsiString text to TMenuItem object?
I've tried :
AnsiString A = "AddUser1";
dynamic_cast<TMenuItem *>(A)->Visible = false;
but does not succeed.

If casting by dynamic_cast is not possible, do we have any other
ways?
I think you are confusing C++ with php or some other script language.
You can't use String values as pointer references to other variables, at
least not in the way you are trying to.
But you could look at the Name property of the object and compare it with a
String. Or compare a pointer as a reference to the memory as it was
originally intended with C++.
Obviously the example you supplied was to demonstrante a concept, but if you
state what exactly you wish to acomplish we could possibly guide you in the
right direction.
-Art
 

{smallsort}

Re:Casting from AnsiString to TMenuItem

"agus" < XXXX@XXXXX.COM >wrote in message
Quote
Hi all, how do I cast an AnsiString text to TMenuItem object?
You can't. You will have to manually loop through all of your items looking
for the one that has the Name you are looking for.
Gambit
 

Re:Casting from AnsiString to TMenuItem

"Hans Galema" wrote in message>That is an impossible operation!
Quote

What do you want ? That the contents of an AnsiString
equals TMeuItem::Caption ?

Thanks for your comment.
I mean, I want to control the visibility of some menu (submenus)
by database. Supposing I have that table below:
MENUTBL
Username varchar(10)
Me{*word*198}scription varchar(60)
Selected varchar(3)
Suppose I have 2 submenu : Add user, Delete user.
and 2 users: A and B. User A can see menu Add user only, User B can
see all menus.
So I'll have 4 rows:
Username Me{*word*198}scription Selected
1. A Add user Yes
2. A Delete user No
3. B Add user Yes
4. B Delete user Yes
To set the visibility of the menus, I guess we only have to loop to
MENUTBL table, see who is current active user, and then set the
visibility according to the menuname. Problem is, the Me{*word*198}scription
field is in Varchar(60) --->AnsiString.
I mean, we can not just say "Add user"->Visible = Yes; can we?
We have to cast this ansistring "Add user" to TMenuItem. Do we have a way
for this?
Thank you. I appreciate your ideas.
 

Re:Casting from AnsiString to TMenuItem

agus wrote:
Quote
Problem is, the Me{*word*198}scription
field is in Varchar(60) --->AnsiString.
I mean, we can not just say "Add user"->Visible = Yes; can we?
No. No can not.
Quote
We have to cast this ansistring "Add user" to TMenuItem.
No. That is impossible. You have been told that already.
Quote
Do we have a way
for this?
If you just give that MemuItem the Name AddUser, you can use:
AddUser->Visible = true;
Otherwise if the Caption of the MenuItem is "Add User" you can
search for that MemuItem and if found use:
MemuItem->Visible = true;
Hans.
 

Re:Casting from AnsiString to TMenuItem

"agus" < XXXX@XXXXX.COM >wrote in message
Quote
I mean, I want to control the visibility of some menu (submenus)
by database. Supposing I have that table below:
MENUTBL
Username varchar(10)
Me{*word*198}scription varchar(60)
Selected varchar(3)
Keep in mind that using the menu description is not possibly the best way to
reference a menu item. Descriptions may change, and you would be forced to
change your code, not a good idea. I would suggest adding a column to your
table that specifies the menu item, independent of a Description. That way
you can change your description (even change the language) and not have to
change the code.
Quote
I mean, we can not just say "Add user"->Visible = Yes; can we?
No. In a compiled language as C++ is, that is not possible because variable
names are resolved at compile time, not at run-time. It's imposible for the
compiler to guess what will be the names of the variables when the
application runs. The kind of feature you are describing is usually only
available on run-time interpreters, such as PHP.
Quote
We have to cast this ansistring "Add user" to TMenuItem. Do we have a way
for this?
It's not a matter of casting, it's a matter of when the variable names are
checked. When the compiler compiles, it doesn't know the runtime values.
In C++ variables are mapped to memory addresses. They are not changed for
you automatically from different types. When you cast a variable, you are
simply telling the compiler to take this data as if it was some other type
of data, but it usually won't change the data itself. Generaly speaking it
keeps pointing to the same location in memory.
Quote
Thank you. I appreciate your ideas.
There are different ways to implement this. For example if you decide
to reference each Menu item in your database with a number (look suggestion
at the beggining), you could put those numbers in the Tag field of your
components, or menu items.
So once you get the data you want from your database, you could simply put
in a loop all your components and check to see if the Tag value is the same
from the one of your database and if it is, then apply the setting. This
aproach would allow you to have one setting in your database be applied to
several components at runtime. You could even take the text from the
description and copy it to the Caption, so if you ever wanted to change the
description of the texts in your menu's, you would only have to change the
value in the database. This would allow for easy translation to other
languages, or custom menus.
Hope that helps.
-Art
 

Re:Casting from AnsiString to TMenuItem

Hello!
How I can create some component name (as AnsiString) and
to perform casting to TMenuItem for example?
Thanks
 

Re:Casting from AnsiString to TMenuItem

"LR" < XXXX@XXXXX.COM >wrote in message
Quote
How I can create some component name (as AnsiString)
and to perform casting to TMenuItem for example?
You can't. You would have to instead loop through every actual component
looking for the one that has the desired Name property value.
Gambit