473,395 Members | 1,937 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,395 software developers and data experts.

C++/CLI System::String question

In .NET (and C++/CLI) there is an overloaded String == operator for
handles. That is when we do comparison of two String handles the
contents of the Strings are compared instead of their addresses.
However how can we do a handle comparison when we want to determine if
we have to do with the same object?
In the style

void somefunc(String ^h1, String ^h2)
{

// We want to see if it is the same object
if(?h1==?h2)
return;
}

--
Ioannis Vranos
Nov 17 '05 #1
8 2280
Ioannis Vranos wrote:
In .NET (and C++/CLI) there is an overloaded String == operator for
handles. That is when we do comparison of two String handles the
contents of the Strings are compared instead of their addresses.
However how can we do a handle comparison when we want to determine if
we have to do with the same object?
In the style

void somefunc(String ^h1, String ^h2)
{

// We want to see if it is the same object
if(?h1==?h2)
return;
}


I would think h1->ReferenceEquals(h2) should work.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
void Check(String^% s1, String^% s2)
{
interior_ptr<String^> p1 = &s1;
interior_ptr<String^> p2 = &s2;
Console::WriteLine(p1==p2);
}

void _tmain()
{
String^ s1 = "hello";
String^ s2 = "hello";

Check(s1,s2); //false
Check(s1,s1); //true
}

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
http://blog.voidnish.com /* My blog on C++/CLI, MFC, Whidbey, CLR... */
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
In .NET (and C++/CLI) there is an overloaded String == operator for
handles. That is when we do comparison of two String handles the
contents of the Strings are compared instead of their addresses.
However how can we do a handle comparison when we want to determine if
we have to do with the same object?
In the style

void somefunc(String ^h1, String ^h2)
{

// We want to see if it is the same object
if(?h1==?h2)
return;
}

--
Ioannis Vranos

Nov 17 '05 #3
Doug Harrison [MVP] wrote:
I would think h1->ReferenceEquals(h2) should work.

OK thanks, I did not know that such a method exists.


--
Ioannis Vranos
Nov 17 '05 #4
Neither did I :-( [Ignore my reply please]

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
http://blog.voidnish.com /* My blog on C++/CLI, MFC, Whidbey, CLR... */
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
Doug Harrison [MVP] wrote:
I would think h1->ReferenceEquals(h2) should work.

OK thanks, I did not know that such a method exists.


--
Ioannis Vranos

Nov 17 '05 #5
Doug Harrison [MVP] wrote:
I would think h1->ReferenceEquals(h2) should work.

Actually I just checked the documentation and ReferenceEquals is a
static method of Object, so in the example given it would be

if( Object::ReferenceEquals(h1,h2) )
// ...

or
if( String::ReferenceEquals(h1,h2) )
// ...
(since it is inherited in all managed types).


--
Ioannis Vranos
Nov 17 '05 #6
Nishant S wrote:
void Check(String^% s1, String^% s2)
{
interior_ptr<String^> p1 = &s1;
interior_ptr<String^> p2 = &s2;
Console::WriteLine(p1==p2);
}

void _tmain()
{
String^ s1 = "hello";
String^ s2 = "hello";

Check(s1,s2); //false
Check(s1,s1); //true
}


Wow. May you explain me some things on this code? Because I did not
think that some of these would work.
But let's first convert it to 100% C++/CLI code:
void Check(System::String ^ %s1, System::String ^ %s2)
{
using namespace System;

interior_ptr<String^> p1 = &s1;

interior_ptr<String^> p2 = &s2;

Console::WriteLine(p1==p2);
}
int main()
{
using System::String;

String^ s1 = "hello";
String^ s2 = "hello";

Check(s1, s2); //false

Check(s1, s1); //true
}

Questions:

1) void Check(System::String ^ %s1, System::String ^ %s2)

In this as far as I can understand we pass tracking references of
handles. If we make it
void Check(System::String ^s1, System::String ^s2)
both calls in main return false. Why?
Are new String objects created in the call, although handles are used?

2) I can not understand this interior_ptr use. We create interior
pointers to handles of Strings. That is, the handles themselves cannot
move in memory, while Strings themselves can move.

Also the p1==p2 comparison looks like it is a comparison between two
pointers to handles, and not of two pointers to Strings.

3) As far as I can understand the first call could also return true,
since the compiler could use only one "hello" to do the assignment. As
far as I know at these assignments, no String objects are created, but
the handles point to the string literals themselves, or am I wrong?


--
Ioannis Vranos
Nov 17 '05 #7
Ioannis Vranos wrote:
But let's first convert it to 100% C++/CLI code:
void Check(System::String ^ %s1, System::String ^ %s2)
{
using namespace System;

interior_ptr<String^> p1 = &s1;

interior_ptr<String^> p2 = &s2;

Console::WriteLine(p1==p2);
}
int main()
{
using System::String;

String^ s1 = "hello";
String^ s2 = "hello";

Check(s1, s2); //false

Check(s1, s1); //true
}

In other words, I believe the above is erroneous, and the correct one is:
void Check(System::String ^s1, System::String ^s2)
{
using namespace System;

interior_ptr<String^> p1 = &s1;

interior_ptr<String^> p2 = &s2;

Console::WriteLine(*p1 == *p2);
}
int main()
{
using System::String;

String^ s1 = "Hello";
String^ s2 = "hello";

Check(s1, s2); //false

Check(s1, s1); //true
}

s1 now points to a different string literal than s2, and the pointers to
handles are dereferenced to compare the handles themselves.

I am pretty sure that this is the correct approach.


--
Ioannis Vranos
Nov 17 '05 #8
However how can we do a handle comparison when we want to determine if
we have to do with the same object?


You should also be able to cast one or both of the strings to Object^
and compare that. That should compile to a single ceq instruction
rather than a method call to ReferenceEquals.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #9

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

Similar topics

2
by: Zach Tong | last post by:
I recently ran our code through a profiler to determine why it was using so much memory. It turns out the System.String object is taking 95% of the memory. We have considered converting the...
27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
7
by: Ioannis Vranos | last post by:
I had reported this as a bug: Description: Default indexed property of System::String crashes for object with stack semantics. int main()
5
by: Ioannis Vranos | last post by:
When we assign a managed or unmanaged string literal to a String *, is a new String created implicitly in the managed heap? Or in other words, what happens when we assign a string literal to...
7
by: Holger Grund | last post by:
What's special about System::String? For instance, why can't one write a function like using System::String; void foo( const String% ); or what's wrong with: int main() {
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
4
by: John Smith | last post by:
How can I allow someone to cast my C# class into System.String? Is it possible in C#? In C++ I used "operator" keyword to mark C++ member function.
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.