0

Hi,

Look I'm new to C# and I have a working knowledge of VB .Net. I would like to learn C# but I'm having a problem. It's probably a basic problem but, for a console program how to I check what someone has typed in so that the console can respond with a Writeline statement?

3
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by tmantix
0

Hi,

Look I'm new to C# and I have a working knowledge of VB .Net. I would like to learn C# but I'm having a problem. It's probably a basic problem but, for a console program how to I check what someone has typed in so that the console can respond with a Writeline statement?

If they are typing a line:

string s = Console.ReadLine();
            Console.WriteLine(s);
0

Ok thanks but how do i compare it. in vb i would say:

Dim x as string
x = Console.Readline

if x = "Hello" then
console.writeline("Hello")
end if

when i try

string x;
x = console.readline();
if (x == "Hello");

it says you can't compare strings.

0
string x;
x = console.readline();
if (x == "Hello");

it says you can't compare strings.
Hmmm, I thought you could do the "==", but anyway:

private static string CompareStrings(string str1, string str2)
   {

      // compare the values, using the CompareTo method on the first string
      int cmpVal = str1.CompareTo(str2);

      if (cmpVal == 0) // the values are the same
         return "The strings have the same value!";

      else if (cmpVal > 0) // the first value is greater than the second value
         return "The first string is greater than the second string!";

      else // the second string is greater than the first string
         return "The second string is greater than the first string!";
   }

Edited by Reverend Jim: Fixed formatting

0

Would that work because my understanding of it as im reading it is that it assigns a string a numerical value then compares it to another number. Would that not just tell you which word is greater in length?

0

Would that work because my understanding of it as im reading it is that it assigns a string a numerical value then compares it to another number. Would that not just tell you which word is greater in length?

No, it works on the sort order of the string: String.CompareTo

0
string x;
x = console.readline();
if (x == "Hello");

it says you can't compare strings.
Hmmm, I thought you could do the "==", but anyway:

Your Hmmm is correct DdoubleD, The == is an overloaded operator in the string class. Because it is overloaded the compiler can translate it in a string Compare or string Equals method.
This works without a problem :

        string x;
        x = Console.ReadLine();
        if (x == "Hello")
        {
            Console.WriteLine("You typed Hello!");
        }
        else
        {
            Console.WriteLine("You typed something but it was not Hello!");
        }

        Console.ReadKey(); //keep console on screen until keypress

@tmantix remember that C# is case sensitive readline(in VB) is not the same as ReadLine(in C#) VB would not make a fuzz about it in C# it matters and it should!

Edited by mike_2000_17: Fixed formatting

0

@tmantix remember that C# is case sensitive readline(in VB) is not the same as ReadLine(in C#) VB would not make a fuzz about it in C# it matters and it should!

Vb is also case sensitive. Ive tried it but thanks for your help.

So == should work Okay ill try it again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.