473,372 Members | 859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 1817
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
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
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
by: Radde | last post by:
HI, Are ther any pitfalls for dynamic cast in type safe downcasting..
3
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
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
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
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
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
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
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,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.