6 Replies - 5090 Views - Last Post: 03 January 2009 - 04:40 AM Rate Topic: -----

#1 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

best way to string compare for loop test?

Post icon  Posted 01 January 2009 - 11:21 PM

Hi guys I'm new in the C# forum, first post of any kind I think, and a total beginner in C# (background in C and newbie in C++ also). I'm reading my beginner how to book and jumping ahead of the lessons and I have a quick question for you.

I'm making a super simple (IE cheesy) text game and found out early that I can't get away with using '==' to test a char or string value, at least not without something more added to the line of code.

My book hasn't covered anything about if statements so I totally experimented and came up a quick test that worked in my program and I want to see if this is considered 'acceptable' to test for char or string values or if there is a standard way to do this that I haven't seen or thought of yet.
static void Main(string[] args)
        {
            String cont = "y";
            String state1 = "y";
            String state2 = "n";

            while(cont.Equals(state1))
            {
                Console.Clear();

                String playerName;
                //do lots more still stuff
        
               //ask for continue state
              Console.Write("Would you like to try again?  Enter y or n: ");
              cont = Console.ReadLine();}
              }
       }

This post has been edited by badjava: 01 January 2009 - 11:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: best way to string compare for loop test?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: best way to string compare for loop test?

Posted 01 January 2009 - 11:42 PM

Well keep in mind that the equals() method in C# is actually checking equality between two objects and the contents that make them up. In the case of strings here it is looking at the individual characters that make up a string and making a character by character match up. So if both strings contain the same characters and same length, the equals method will then say that the two objects are equal.

So typically you would be using the equals method to compare to objects themselves and it might come out true only if the two objects are the same object.

For things like simple strings you can use the "==" relational operator. Both are right in this case and both will work. When it comes to simple data types like strings most people will go with the "==" and when it comes to comparing objects they will use the equals() method.

Hopefully that got to the bottom of what you were wondering. :)
Was This Post Helpful? 0
  • +
  • -

#3 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: best way to string compare for loop test?

Posted 01 January 2009 - 11:49 PM

View PostMartyr2, on 1 Jan, 2009 - 10:42 PM, said:

For things like simple strings you can use the "==" relational operator. Both are right in this case and both will work. When it comes to simple data types like strings most people will go with the "==" and when it comes to comparing objects they will use the equals() method.

Hopefully that got to the bottom of what you were wondering. :)


I received a compile error when I was using the '==' test for the same little program but it did seem like I could skip the error. I'm also new to VS, I've been using Dev C++ before this so I thought 'uh oh compile error, I better find another way to do that'.

So if I'm following you here most people would just ignore the compiler 'warning' and go ahead with the '==' for the simple state test for the yes/no loop condition?

Hey I just went and changed it back to the '==' test and didn't receive that error this time. I wonder if I had used single quotes (or no quotes?) when referencing the y and n values and caused the error ....in error. :P

Well now I feel kinda dumb, it looks like I had something wrong in the syntax. If I see that error on compile again I'll post the message up and see if someone can tell me for sure what I'm doing wrong.

Thank you martyr2!
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: best way to string compare for loop test?

Posted 02 January 2009 - 06:38 AM

View Postbadjava, on 2 Jan, 2009 - 01:49 AM, said:

View PostMartyr2, on 1 Jan, 2009 - 10:42 PM, said:

For things like simple strings you can use the "==" relational operator. Both are right in this case and both will work. When it comes to simple data types like strings most people will go with the "==" and when it comes to comparing objects they will use the equals() method.

Hopefully that got to the bottom of what you were wondering. :)


I received a compile error when I was using the '==' test for the same little program but it did seem like I could skip the error. I'm also new to VS, I've been using Dev C++ before this so I thought 'uh oh compile error, I better find another way to do that'.

So if I'm following you here most people would just ignore the compiler 'warning' and go ahead with the '==' for the simple state test for the yes/no loop condition?

Hey I just went and changed it back to the '==' test and didn't receive that error this time. I wonder if I had used single quotes (or no quotes?) when referencing the y and n values and caused the error ....in error. :P

Well now I feel kinda dumb, it looks like I had something wrong in the syntax. If I see that error on compile again I'll post the message up and see if someone can tell me for sure what I'm doing wrong.

Thank you martyr2!


You should post the code with the "==".

You should also post the compiler error.

I use the "==" and I never have a compiler error. So there is something else wrong with your code. Post it and we can help you.

Just for fun, this is what your code should look like if you were using "==":

static void Main(string[] args)
{
	String cont = "y";
	String state1 = "y";
	String state2 = "n";

	while (cont == state1)
	{
		Console.Clear();

		String playerName;
		//do lots more still stuff

		//ask for continue state
		Console.Write("Would you like to try again?  Enter y or n: ");
		cont = Console.ReadLine();
	}
}



Now I do get two compiler warnings(which are different from compiler errors). You never use "state2" and "playerName", so it gives you a warning stating that you are creating two objects that you never use.
Was This Post Helpful? 0
  • +
  • -

#5 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: best way to string compare for loop test?

Posted 03 January 2009 - 04:20 AM

View Posteclipsed4utoo, on 2 Jan, 2009 - 05:38 AM, said:

I use the "==" and I never have a compiler error. So there is something else wrong with your code. Post it and we can help you.

I was going to paste in the changes I made after the last couple of posts to show you how I got it working with the '==' without errors but I cannot find any trace of the code on my system at this time. So a big fuck you very much to VS gotten damnit microsoft i hate u. Doing a keyword search on every file on my HD to see if I can find a sniff of wtf happened to it.

This post has been edited by badjava: 03 January 2009 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: best way to string compare for loop test?

Posted 03 January 2009 - 04:31 AM

View Postbadjava, on 3 Jan, 2009 - 03:20 AM, said:

View Posteclipsed4utoo, on 2 Jan, 2009 - 05:38 AM, said:

I use the "==" and I never have a compiler error. So there is something else wrong with your code. Post it and we can help you.

I was going to paste in the changes I made after the last couple of posts to show you how I got it working with the '==' without errors but I cannot find any trace of the code on my system at this time. So a big fuck you very much to VS gotten damnit microsoft i hate u. Doing a keyword search on every file on my HD to see if I can find a sniff of wtf happened to it.


This is fucked up, VS somehow trashed my project, I found backup files named: ~AutoRecover.Program.cs
in a directory: Visual Studio 2008\backup files\Adventure1
Adventure1 was my project and game that VS just tossed in the trash for some reason. I hope I can get the damn thing back or it's one nights work wasted.

[edit] I was able to get my code from the one .cs file that was left of the project, whew. I'll just make a new project and paste in the code and make sure to save 2-3 copies in different places like a thumb drive etc. for the next time VS tries to screw me over. GRRrrrrr
Was This Post Helpful? 0
  • +
  • -

#7 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: best way to string compare for loop test?

Posted 03 January 2009 - 04:40 AM

View Posteclipsed4utoo, on 2 Jan, 2009 - 05:38 AM, said:

You should post the code with the "==".

You should also post the compiler error.

OK, finally I can show you how I changed my code after the previous comments before your post and got the '==' to work. There must have been a typo or something in the way I set this up originally with ' ' instead of " " or something else dumb like that but it's working now! I'm lacking any case testing or error testing for bad user input of course, this is just a learning exercise for me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adventure1
{
    class Program
    {
        static void Main(string[] args)
        {
            String cont = "y";

            while(cont=="y")
            {
                Console.Clear();
                String playerName;
                String travel;
                String direction1 = "n";
                String direction2 = "s";
                String direction3 = "e";
                String direction4 = "w";

                //do lots more stuff

            Console.WriteLine("Congratulations, {0}, this is the end of your adventure.  Thank you for playing.", playerName);
            Console.Write("Would you likes to try again?  Enter y or n: ");
            cont = Console.ReadLine();}
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1