473,480 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dynamic runtime casting?

Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );

Larry
May 19 '06 #1
6 1822
look up "dynamic_cast"
"Larry Minton" <av*****@jadeinc.com> wrote in message
news:gq********************************@4ax.com...
Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );

Larry

May 19 '06 #2
to use dynamic_cast requires specifying at compile time the class to
cast to. We don't have that, we are determining the class we want to
cast to at runtime.

return intern(dynamic_cast<l_pInterface^>(l_pObject));

--> error C2061: syntax error : identifier 'l_pInterface'

Larry

On Fri, 19 May 2006 10:52:03 +0800, <www.fruitfruit.com> wrote:
look up "dynamic_cast"
"Larry Minton" <av*****@jadeinc.com> wrote in message
news:gq********************************@4ax.com.. .
Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );

Larry


May 19 '06 #3
Larry Minton wrote:
Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.
dynamic_cast is identical to the VB TryCast function - they both compile to
the same IL.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );


What exactly are you trying to return here? How do you plan to return an
object reference of unspecified type? AFIAK, the best you can do is just
plain 'object'. .NET is not like COM where there's a universal base
interface (IUnknown). All you have is a univeral object reference
(System::Object).

If you have an instance of System::Type and you want to know if a given
System::Object is reference convertible to that type, you can use
Type::IsInstanceOfType to determine if ths object is an instance of that
type.

HTH

-cd
May 19 '06 #4
If the type to cast to is only known at runtime you can use:

return l_pType.IsInstanceOfType(l_pObject) ? l_pObject : nullptr;

Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );


--
Vladimir Nesterovsky
May 19 '06 #5
Ah, ok, thanks. I was thinking of it as returning a "different" object
(i.e., running GetType() on the object would return the interface
type) when it is always still just the same object. I'll need to
adjust my thinking...

Thanx, Larry

On Fri, 19 May 2006 10:02:35 +0200, "Vladimir Nesterovsky"
<vl******@nesterovsky-bros.com> wrote:
If the type to cast to is only known at runtime you can use:

return l_pType.IsInstanceOfType(l_pObject) ? l_pObject : nullptr;

Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );


May 19 '06 #6
Carl,

The intern method actually wraps the object in another object (a
DotNetObject), and that outer object is what is used by the scripting
engine in an object type it understands (a dotNetObject). This is for
a C++ application with a typeless scripting language, we are adding
..net access to the scripting language. So the user can call inspection
functions on the dotNetObject, and via reflection we expose the
methods/properties/events/etc of the wrapped .net object.

We already have the concept of interfaces in the scripting language,
where you can get interfaces that are exposed by our app's objects and
operate on those interfaces. I'm trying to extend that concept to the
..net exposure. I'm thinking that I'll need to tweak the DotNetObject
so that I optionally specify the Type to expose it as, rather than
just take the Type from the object. So then we would have:

myString = dotNetObject "System.String" "AAA"
showMethods myString -- shows all methods of String
newString = myString.Copy()

iclonable = getInterface myString "IClonable"
showMethods iclonable -- shows just that methods of IClonable
interface
clonedString = iclonable.Clone()

Our current "interfaces" are actually objects, which colors my
thinking here.

Larry

On Thu, 18 May 2006 21:19:48 -0700, "Carl Daniel [VC++ MVP]"
<cp*****************************@mvps.org.nospam > wrote:
Larry Minton wrote:
Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.


dynamic_cast is identical to the VB TryCast function - they both compile to
the same IL.

For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:

array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface) );


What exactly are you trying to return here? How do you plan to return an
object reference of unspecified type? AFIAK, the best you can do is just
plain 'object'. .NET is not like COM where there's a universal base
interface (IUnknown). All you have is a univeral object reference
(System::Object).

If you have an instance of System::Type and you want to know if a given
System::Object is reference convertible to that type, you can use
Type::IsInstanceOfType to determine if ths object is an instance of that
type.

HTH

-cd


May 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2151
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from...
5
1990
by: Thomas Hede Jensen | last post by:
Hi, I'm currently working on a project in which I link to a library where the function definitions takes parameters of the type: "const int *const *paramName" As far as I can tell from the...
5
7988
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
3
1312
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
10
5730
by: Yechezkal Gutfreund | last post by:
I have two subclasses of SpriteModel (1) LocalSprite (2)Sprite Both implement a method called .ToXml() which returns an XmlDocument. But they are different. I instances of these objects...
2
673
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a...
6
6371
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
6
4079
by: DaTurk | last post by:
Hi, I have three interfaces lets call them parent, child-A, and child-B. I want to have one variable, that can be either child-A, or child-B at run time. I just can't seem to figure out how to...
13
3019
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
3
2090
by: Trev | last post by:
Hi, I have a series of functions which do the following: ValidateData( args ); //args if of type ArrayList // Work out if the data is valid or not, and work out the type of args - int, string,...
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.