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?

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);

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.

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!";
   }

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?

also

if (s.Equals("Hello"))
                bool b = true;

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

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!

@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.

Thanks ddanbe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.