why does this if statement fail ? 
Author Message
 why does this if statement fail ?

String *sRP = ""; // managed .net string

sRP = Console::ReadLine();

if (sRP == "y" || sRP == "Y")

{

Quote:
}

Always skips as if y/Y was not pressed. thanks, Craig


Mon, 13 Sep 2004 22:40:29 GMT  
 why does this if statement fail ?
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")
== 0)) {
         Console::WriteLine(S"Done!");
         break;
      }

     Console::WriteLine(S"Try Again!");
   }

Quote:
}

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.


Quote:
> String *sRP = ""; // managed .net string

> sRP = Console::ReadLine();

> if (sRP == "y" || sRP == "Y")

> {

> }

> Always skips as if y/Y was not pressed. thanks, Craig



Tue, 14 Sep 2004 00:51:12 GMT  
 why does this if statement fail ?
Jonathan,

thank you very much ! makes sense. anyway to create an instance of the
String object ? or is this not required ?

Craig



Quote:
> 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")
> == 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



Tue, 14 Sep 2004 00:59:09 GMT  
 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")

- Show quoted text -

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



Tue, 14 Sep 2004 01:40:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. SQLGetData Fails on Multiple Active Statement Handles with SQL Server 6.5

2. Why is it doing this?

3. Why does doing this break my program?

4. Why are most HPC done in Fortran?

5. Why are most HPC done

6. C array syntax proposal (was: Why are most HPC done)

7. Why is curses doing this????

8. C# Strange if{} statement behavior, why?

9. Why method not showing up from #import statement

10. why no braces for single statement blocks?

11. re : why no braces for single statement blocks?

12. Newbie: Why VBScript statements cannot work in project Settings

 

 
Powered by phpBB® Forum Software