473,545 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2295
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->ReferenceEqual s(h2) should work.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
void Check(String^% s1, String^% s2)
{
interior_ptr<St ring^> p1 = &s1;
interior_ptr<St ring^> p2 = &s2;
Console::WriteL ine(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.thi s.grad.com> wrote in message
news:OO******** ******@TK2MSFTN GP10.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->ReferenceEqual s(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.thi s.grad.com> wrote in message
news:Op******** ******@tk2msftn gp13.phx.gbl...
Doug Harrison [MVP] wrote:
I would think h1->ReferenceEqual s(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->ReferenceEqual s(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::Referen ceEquals(h1,h2) )
// ...

or
if( String::Referen ceEquals(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<St ring^> p1 = &s1;
interior_ptr<St ring^> p2 = &s2;
Console::WriteL ine(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::S tring ^ %s1, System::String ^ %s2)
{
using namespace System;

interior_ptr<St ring^> p1 = &s1;

interior_ptr<St ring^> p2 = &s2;

Console::WriteL ine(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::S tring ^ %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::S tring ^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::S tring ^ %s1, System::String ^ %s2)
{
using namespace System;

interior_ptr<St ring^> p1 = &s1;

interior_ptr<St ring^> p2 = &s2;

Console::WriteL ine(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::S tring ^s1, System::String ^s2)
{
using namespace System;

interior_ptr<St ring^> p1 = &s1;

interior_ptr<St ring^> p2 = &s2;

Console::WriteL ine(*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
460
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 strings to StringBuilder objects, but I don't think this will help. From what I understand, the StringBuilder only helps with speed increases (by...
27
51663
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 have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found...
5
10025
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
1490
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
1650
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 TextBox::Text and then writing in this TextBox?
7
7816
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
5018
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 because in further editions of .net this will be deprecated or so on.. . so, please, anybody can tellme how to convert from System::String::^ to...
4
3866
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
7458
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 methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as...
0
7479
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7669
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7439
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.