
why does this if statement fail ?
You can do
String *pYes = new String("y");
or:
String *pYes = new String(S"y");
--
Jonathan Caves
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Quote:
> Jonathan,
> thank you very much ! makes sense. anyway to create an instance of the
> String object ? or is this not required ?
> Craig
> > In your code sRP is a pointer to a String object: not a instance of a
> String
> > object so the expression
> > sRP == "y"
> > will compare the address of your String object to the address of the C++
> > string literal "y" -- something that is always guaranteed to be false.
> > What you need to do is to compare the contents of the strings - for
> example:
> > #using <mscorlib.dll>
> > using namespace System;
> > int main()
> > {
> > while (true) {
> > String *pResponse = Console::ReadLine();
> > if ((pResponse->CompareTo(S"y") == 0) ||
(pResponse->CompareTo(S"Y")
Quote:
> > == 0)) {
> > Console::WriteLine(S"Done!");
> > break;
> > }
> > Console::WriteLine(S"Try Again!");
> > }
> > }
> > Note: I have also changed the string literals from C++ string literals
to
> > meta-data string literals -- this is an optimization as the compiler
does
> > not have to create temporary String objects to represent the C++ string
> > literals.
> > --
> > Jonathan Caves
> > Microsoft Corporation
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
> > > String *sRP = ""; // managed .net string
> > > sRP = Console::ReadLine();
> > > if (sRP == "y" || sRP == "Y")
> > > {
> > > }
> > > Always skips as if y/Y was not pressed. thanks, Craig